我有以下型号:
Product
BaseProduct
BaseCode
Series
Run Code Online (Sandbox Code Playgroud)
他们每个人都是一个独立的实体,但他们是相关的.
Product有一个外键BaseProduct,BaseProduct有一个外键BaseCode和BaseCode一个外键Series.
我在BaseCode模型中指定了一个方法,因此我可以选择Products与该特定内容相关的所有方法BaseCode:
public function Products()
{
return $this->hasManyThrough('Product', 'BaseProduct', 'BaseCodeId', 'BaseProductId');
}
Run Code Online (Sandbox Code Playgroud)
BaseCodeId是BaseCode和BaseProduct那个指向那个PK 的FK的PK,并且BaseProductId是PK的BaseProduct,在Product表中,有一个指向它的FK.
这一切都很好,对我来说只是花花公子.
我想更进一步,并在我的Series模型中定义如下内容:
public function Products()
{
//It's here that I'd have no idea what to do - is there something like a three-level deep "hasManyThroughThrough" or something?
return $this->BaseProducts()-> /* and then whatever I'd do here to get the products */
}
public function BaseProducts()
{
return $this->hasManyThrough('BaseProduct', 'BaseCode', 'SeriesId', 'BaseCodeId');
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能获得Product与之相关的所有内容Series?
我对4.1有点新,但它看起来好像hasManyThrough()不是为你正在寻找的关系类型而设计的,而且实际上只是两级深度加载的快捷方式.
尝试传统的渴望加载......
$data = Product::with('BaseProduct.BaseCode.Series');
您需要确保在模型中根据需要设置关系,而不是调用模型名称,您将需要调用定义关系的方法.
显然,请确保您的模型通过使用protected $primaryKey = 'SeriesID'等知道主键是什么...
| 归档时间: |
|
| 查看次数: |
1384 次 |
| 最近记录: |