如何在Laravel中加入一对多关系

时间:2017-02-16 10:31:05

标签: php mysql laravel eloquent

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

我正在使用以下相关的3个模型/表格,客户,商业和网站:

在Customer.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');
  }
}

在Business.php中:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

在Website.php中:

Customer

因此,Businesses可以包含多个Websites,其中可以有多个$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(); 。 现在,我正在尝试使用join语句返回客户及其相关业务和网站信息的列表。我正在使用以下代码返回此信息:

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

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

$customer1

如果客户的某个企业有一个网站,但假设[ 0 => { $customer1Data, $customer1BusinessData1, $customer1WebsiteData } 1 => { $customer1Data, $customer1BusinessData2, $customer1WebsiteData } ... ] 有两个企业,那么上述联接会返回以下格式的内容:

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

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

=IIf
(Parameters!THE_DATE.Value Is
Nothing,Nothing,Format(Parameters!THE_DATE.Value,"yyyy-MM-dd"))

有没有办法用join语句实现这个目的?或者我应该以不同的方式接近这个?非常感谢任何帮助,非常感谢。

2 个答案:

答案 0 :(得分:0)

根据@Rooshan Akthar的评论,由于您创建的关系,您可以使用雄辩的方式执行以下操作:

$customer->businesses->websites;

您也可以在没有调用上述任何一项的情况下前往{{1}},laravel将为您获取这些关系。

答案 1 :(得分:0)

在我开始解释雄辩的模型关系之前,请阅读: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();

我希望这有帮助!