Laravel,其中条件不适用于具有关系的查询
我有一个简单的查询,如下所示:Laravel,其中条件不适用于具有关系的查询,laravel,eloquent,Laravel,Eloquent,我有一个简单的查询,如下所示: $category = Category::with('translation') ->with(['childCategories' => function ($query) { $query->active(); }]) ->where('id', $id)->first(); 范围和关系: public function s
$category = Category::with('translation')
->with(['childCategories' => function ($query) {
$query->active();
}])
->where('id', $id)->first();
范围和关系:
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function childCategories()
{
return $this->hasManyThrough('App\SupremeShop\Models\Category',
'App\SupremeShop\Models\CategoryToCategory',
'id_parent_category', //category_to_categories
'id', //categories
'id',
'id_category'); // category_to_categories.
}
所以我在寻找类别,一些翻译和给定“主”类别的子类别。查询返回15个子类别。但在查询中,应该只取活动的,并且不能正常工作。当我使用dd时,它还显示非活动的子类别。我试图删除作用域并在其中编写simple,但结果是一样的
有人知道为什么情况不正常吗 我认为问题在于该列不明确,因为
Category
及其关系childCategories
都是相同的模型,并且共享相同的表
active()
作用域将应用于父类别
,而不是子类别
试试看这是否有效
$category=category::from('categories as c')
->使用(['childCategories'=>函数($query){
$query->active();
}])
->其中('id',$id)->first();