如何在模型中调用作用域?

How to call scope in model?

我在模型中有以下方法:

public function announcements()
    {
        return $this->categories()->with("announcements");
    }

在同一个模型中:

public function scopeActive($query)
    {
        return $query->where('votes', '>', 100);
    }

在以下模型中调用此本地作用域很热门:

return $this->categories()->with("announcements")->active(); ?

我假设类别是这个模型上的关系,公告是类别模型上的关系。

那你可以试试:

return $this->active()->categories->with("announcements")

$this->active() 将 return 该模型的活动记录。

->categories 将获得相关类别

->with("announcements")会预先加载所有类别的公告。

这将 return 一个 eloquent 查询生成器实例。