【问题标题】:has one through relationship laravel有一个通过关系 laravel
【发布时间】:2017-10-18 19:19:49
【问题描述】:

我在这里阅读:

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 可能为空并且我什么也不返回,所以我收到一个错误:“Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation”。这样做的正确 laravel 方法是什么?

【问题讨论】:

  • $this->hasOne(Manufacturer::class); hasOne hasMany belongsTo 等等返回Illuminate\Database\Eloquent\Relations\Relation 告诉你在尝试之前首先建立关系 >相互关联.

标签: laravel laravel-5 eloquent


【解决方案1】:

hasOneThrough 关系已添加到版本 5.8

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

【讨论】:

    【解决方案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' . . . . . .
    

    第二个是Manufacturer模型,有kohler company, kohler and 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' . . . . . .
    

    作为层次结构的最后和底部,我们有 Product 模型..它也应该属于制造商..所以在模型中我们添加了一个功能

    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();
    

    这将退回所有产品及其相应的制造商以及该制造商的捆绑位置。没有制造商的也将其制造商留空。

    【讨论】:

    • 这一切都非常正确,但它没有回答,我该怎么做:$product->manufacturer_bundle 是的,您的答案是性能优化,可以使用->with 函数检索记录。但是我不想做的只是$product->manufacturer->manufacturer_bundle我想直接能够做到$product->manufacturer_bundle
    【解决方案3】:

    我想你在找model mutators

    在你的情况下,我应该选择accessor

    例如:

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

    【讨论】:

    • 它不是一个属性。我的意思是,这能行吗?!但它看起来更像是一个 hack,因为它真的不是一个属性
    猜你喜欢
    • 2023-03-04
    • 2015-07-30
    • 1970-01-01
    • 2022-01-21
    • 2019-06-27
    • 2020-01-15
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多