【发布时间】:2019-04-27 13:35:59
【问题描述】:
我正在处理一个复杂的购物车项目。我有这样的关系
类别组模型
// App\CategoryGroup
class CategoryGroup extend Model
{
public function categories()
{
return $this->hasMany(Category::class);
}
}
类别模型
// App\Category
class Inventory extend Model
{
public function categoryGroup()
{
return $this->belongsTo(CategoryGroup::class);
}
public function products()
{
return $this->belongsToMany(Product::class);
}
public function listings()
{
return $this->belongsToMany(
Inventory::class,
'category_product',
null,
'product_id',
null,
'product_id'
);
}
}
产品型号
// App\Product
class Product extend Model
{
public function categories()
{
return $this->belongsToMany(Category::class);
}
public function listings()
{
return $this->hasMany(Inventory::class);
}
}
库存模型
// App\Inventory
class Inventory extend Model
{
public function products()
{
return $this->belongsTo(Product::class);
}
}
现在我被困在需要像这样在 CategoryGroups 和 Inventory 模型之间创建关系的情况:
// App\CategoryGroup
class CategoryGroup extend Model
{
public function categories()
{
return $this->hasMany(Category::class);
}
public function listings()
{
// Can't figured out the way
// A belongsToMany like the App\Category would be great
}
}
有没有什么好的方法可以实现这种关系?
【问题讨论】:
标签: laravel-5 eloquent laravel-5.5 laravel-query-builder eloquent-relationship