gpt4 book ai didi

php - 如何在 Laravel 中通过 join 将一对多关系分组

转载 作者:行者123 更新时间:2023-11-29 19:23:57 25 4
gpt4 key购买 nike

我正在使用 Laravel 5.3 并尝试使用联接从多个表返回数据。

我正在使用 3 个模型/表:客户、业务和网站,它们的相关关系如下:

在 Customer.php 中:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
public function businesses()
{
return $this->hasMany('App\Business');
}
}

在 Business.php 中:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Business extends Model
{
public function customer()
{
return $this->belongsTo('App\Customer');
}

public function websites()
{
return $this->hasMany('App\Website');
}
}

在 Website.php 中:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Website extends Model
{
public function business() {
return $this->belongsTo('App\Business');
}
}

因此,一个客户可以拥有许多企业,而这些企业又可以拥有许多网站。现在,我尝试使用 join 语句返回客户列表及其相关业务和网站信息。我使用以下代码返回此信息:

$customers = \DB::table('customers')
->join('businesses', 'customers.id', '=', 'businesses.customer_id')
->join('websites', 'businesses.id', '=', 'websites.business_id')
->select('customers.x', 'businesses.y', 'websites.z')
->get();

我希望数据以关联客户数组的数组形式返回,业务和网站数据嵌套在关联数组中,如下所示:

[
0 => {
$customer1Data,
$customer1BusinessData,
$customer1WebsiteData
}
1 => {
$customer2Data,
$customer2BusinessData,
$customer2WebsiteData
}
...
]

如果客户有一个企业,该企业有一个网站,但假设 $customer1 有两个企业,则上述联接会返回以下格式的内容:

[
0 => {
$customer1Data,
$customer1BusinessData1,
$customer1WebsiteData
}
1 => {
$customer1Data,
$customer1BusinessData2,
$customer1WebsiteData
}
...
]

有没有办法修改 join 语句以这种格式返回该场景:

[
0 => {
$customer1Data,
businesses => {
$customer1BusinessData1,
$customer1BusinessData2
}
...
}
]

有没有办法可以通过 join 语句实现此目的?或者我应该以不同的方式处理这个问题?任何帮助将不胜感激,非常感谢。

最佳答案

在我深入解释 Eloquent 模型关系之前,请阅读以下内容:https://laravel.com/docs/5.4/eloquent 。这将使事情变得清晰。

在模型关系中,您可以指定将这两个表链接在一起的数据库字段名称。这甚至适用于数据透视表。有关基本文档,请阅读:https://laravel.com/docs/5.4/eloquent-relationships#defining-relationships

例如在客户模型中:

public function businesses() 
{
return $this->hasMany(Business::class, 'id', 'customer_id');
}

public function websites()
{
//Argument order: final class, pivot class, pivot foreign key, foreign key final model, primary key start model (customer)
return $this->hasManyThrough(Website::class, Business::class, 'customer_id', 'id', id);
}

如果最后一部分有点难以理解,请阅读文档:https://laravel.com/docs/5.4/eloquent-relationships#has-many-through

对于商业模式:

public function website()
{
return $this->hasMany(Website::class, 'id', 'business_id');
}

现在您可以使用 eloquent 检索数据。

//This will retrieve all customers with all of their website information.
Customer::with('websites')->get();

//This will retrieve all customers with their business information.
Customer::with('businesses')->get();

//This will retrieve all customers with business and website information
//Retrieves: [
// customer: [
// customerDetails: []
// businesses: [ b1, b2],
// websites: [w1, w2]
// ]
//]
Customer::with('businesses', 'websites')->get();

//This will retrieve all customers with business information and the website information for each business
//Retrieves: [
// customer: [
// customerDetails: []
// businesses: [
// b1: [ websites: [w1] ],
// b2: [ websites: [w2] ]
// ],
// ]
//]
Customer::with('businesses.websites')->get();

希望这会有所帮助!

关于php - 如何在 Laravel 中通过 join 将一对多关系分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com