【发布时间】:2017-05-31 11:47:20
【问题描述】:
我在树中有彼此相关的类别。每个类别hasMany儿童。每个终端类别hasMany 产品。
产品还有belongsToMany不同的类型。
我想用他们的孩子和产品急切地加载类别,但我也想设置一个条件,即产品属于某种类型。
这就是我的类别模型的样子
public function children()
{
return $this->hasMany('Category', 'parent_id', 'id');
}
public function products()
{
return $this->hasMany('Product', 'category_id', 'id');
}
产品模型
public function types()
{
return $this->belongsToMany(type::class, 'product_type');
}
在我的数据库中,我有四个表: 类别、产品、类型和产品类型
我尝试过像这样急切加载,但它会加载所有产品,而不仅仅是满足条件的产品:
$parentLineCategories = ProductCategory::with('children')->with(['products'=> function ($query) {
$query->join('product_type', 'product_type.product_id', '=', 'product.id')
->where('product_type.type_id', '=', $SpecificID);
}]])->get();
【问题讨论】: