有一个通过关系laravel

时间:2017-05-18 20:04:00

标签: laravel laravel-5 eloquent

我在这里读到:

Has one through Laravel Eloquent

但它没有解决问题

我们有制造商名称,例如:

kohler company
kohler
Kohler US

这些名称我们用一个干净的名字捆绑在一起 - > Kohler

所以我们有一张桌子

manufacturer

和一个

manufacturer_bundle

这些制造商创建了products

所以产品有制造商,制造商与制造商捆绑链接。

但是:并非所有产品都有制造商进入。它可以是空的。或者制造商未分配给制造商捆绑包。

我想要一个功能

$product->manufacturer_bundle

目前我必须做

$product->manufacturer->manufacturer_bundle

我无法写一个函数

public function manufacturer_bundle(){
  return $this->manufacturer->manufacturer_bundle;
}

因为$this->manufacturer可能为空而我什么也没有返回,我得到一个错误:"关系方法必须返回一个类型为Illuminate \ Database \ Eloquent \ Relations \ Relation" 。这样做的正确方法是什么?

3 个答案:

答案 0 :(得分:2)

好的..你有三个型号..

首先是 ManufacturerBundle 模型作为层次结构的峰值,在里面添加一个函数

public function manufacturers(){
    return $this->hasMany('App\Manufacturers', 'bundle_id');
}

public function products(){
    return $this->hasManyThrough('App\Products','App\Manufacturer','bundle_id','manufacturer_id');
}

模型表结构必须具有:

'id' as primary key
'name' as the name of the manufacturer bundle
'and so on' . . . . . .

第二个是制造商模型,它有kohler公司,kohler和Kohler US等数据。这个模型必须属于manufacturer_bundle ..所以我们在模型里面做一个像

public function bundle(){
    return $this->belongsTo('App\ManufacturerBundle');
}

所以这也必须有它拥有的产品..所以

public function products(){
    return $this->hasMany('App\Products','manufacturer_id');
}

模型表结构必须具有:

'id' as primary key
'bundle_id' as the model manufacturerbundle id
'name' as the name of the manufacturer
'and so on' . . . . . .

作为层次结构的最后和最底层,我们有产品模型..它也应该属于制造商..所以在模型中我们添加一个函数

public function manufacturer(){
    return $this->belongsTo('App\Manufacturer');
}

模型表结构必须具有:

'id' again as primary key
'manufacturer_id' as the model manufacturer id
'name' as product name
'and so on' . . . . . . fields you want to add

所以我们现在设定了三个模型的关系。你有多种方法可以打电话给他们

首先使用捆绑包..

$bundles = ManufacturerBundle::with('manufacturers.products')->get();
  

这将返回所有捆绑包,包括它的制造商及其产品..

$bundles = ManufacturerBundle::with('products')->get();
  

这将返回所有捆绑包,包括属于该捆绑包的制造商的所有产品..

第二是产品

$products = Product::with('manufacturer.bundle')->get();
  

这将返回所有产品及其相应的制造商以及捆绑此制造商的地方。那些没有制造商的人也会把它的制造商留空。

答案 1 :(得分:1)

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 216 145"><defs><style>.cls-1{fill:black;}</style></defs><g id="Layer_2" data-name="Layer 2"><g id="Logo"><g id="Logo_1" data-name="Logo 1"><path id="Tag_R" class="cls-1" d="M160.91,48.19h0a5.18,5.18,0,0,0,1.69,3.9l40.5,38.6L162.62,129a5.14,5.14,0,0,0-1.71,3.92v.72c0,4.1,4.12,6.38,6.91,3.82l46.5-42.73a5.18,5.18,0,0,0,0-7.64l-46.5-42.72C165,41.81,160.91,44.09,160.91,48.19Z"/><path id="Tag_L" class="cls-1" d="M55.09,48.53h0a5.2,5.2,0,0,1-1.69,3.91L12.9,91l40.48,38.3a5.16,5.16,0,0,1,1.71,3.92V134c0,4.1-4.12,6.38-6.91,3.82L1.68,95.07a5.17,5.17,0,0,1,0-7.63l46.5-42.73C51,42.15,55.09,44.43,55.09,48.53Z"/><g id="Brush"><path class="cls-1" d="M115,44.12c.7,9.7,12.05,11,19.74,10.59,5.76-.32-38.44,72.41-50.48,89.72-.94,1.36-2.22.09-1.7-1.68C89.13,120.48,114.38,35.67,115,44.12Z"/><path class="cls-1" d="M121.67,24.62c6.79-7.1,23.19-16,25.94-24.62,0,0,1.9,50.74-15.16,51.3C119.81,51.71,109.72,37.09,121.67,24.62Z"/></g><path id="Eye_R" class="cls-1" d="M147.68,75c-4.74,0-8.59,4.31-8.59,9.61s3.85,9.61,8.59,9.61,8.58-4.3,8.58-9.61S152.42,75,147.68,75Zm0,13.6a4,4,0,1,1,3.56-4A3.79,3.79,0,0,1,147.68,88.61Z"/><path id="Eye_L" class="cls-1" d="M68.87,75c-4.74,0-8.59,4.31-8.59,9.61s3.85,9.61,8.59,9.61,8.58-4.3,8.58-9.61S73.61,75,68.87,75Zm0,13.6a4,4,0,1,1,3.56-4A3.8,3.8,0,0,1,68.87,88.61Z"/></g></g></g></svg>关系已在5.8版中添加

https://laravel.com/docs/5.8/eloquent-relationships#has-one-through

答案 2 :(得分:0)

我认为您正在寻找model mutators

在你的情况下我应该去accessor

例如:

public function getManufacturerBundleAttribute()
{
    return $this->manufacturer->manufacturer_bundle;
}