2017-02-28 46 views
1

我在模型下面的方法:如何在模型中调用范围?

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

而且在同一个模型:

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

热调用这个局部范围,型号为:

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

“类别”的方法是什么样的?它是否返回查询生成器实例? – camelCase

回答

1

我承担类别是这种模型的关系,通告是类别模型的关系。

这时可以尝试:

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

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

->categories将获得相关的类别

->with("announcements")将贪婪加载所有类别的公告。

这将返回一个雄辩的查询生成器实例。

+0

请格式化您的答案 – Darama