在laravel中合并两个hasOne关系-雄辩
我有两个表,一个是产品,另一个是相关产品 其中相关产品与products.id连接,有时与product.parent\u id连接在laravel中合并两个hasOne关系-雄辩,laravel,laravel-5.2,laravel-5.1,Laravel,Laravel 5.2,Laravel 5.1,我有两个表,一个是产品,另一个是相关产品 其中相关产品与products.id连接,有时与product.parent\u id连接 public function relationWithId() { return $this->hasOne('App\RelatedProduct', 'product_id', 'id'); } public function relationWithParent() { return $this->hasOne('App\Rel
public function relationWithId()
{
return $this->hasOne('App\RelatedProduct', 'product_id', 'id');
}
public function relationWithParent()
{
return $this->hasOne('App\RelatedProduct', 'product_id', 'parent_id');
}
所以需要一个第三个关系,我可以像这样将两者合并
public function relationProduct()
{
/*****/
}
Product::where('status', '=', 'active')->with('relationProduct')->get();
提前感谢尝试创建一个访问器,该访问器合并两个关系并返回集合
public function getRelatedProductAttribute($value){
$withId = $this->relationWithId;
$withParentId = $this->relationWithParent;
return $withId->push($withParentId);
}
当我使用Product::where('status','=','publish')->with('getRelatedProductAttribute')->get()时;行得通吗?不,我想行。像这样尝试
$products->相关产品代码>