Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel,其中条件不适用于具有关系的查询_Laravel_Eloquent - Fatal编程技术网

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();