Laravel 5.3范围链未按预期工作

时间:2017-01-02 20:27:27

标签: laravel-5.3

我正在链接两个范围。我希望模型只返回符合BOTH范围内约束的行。

public function scopeHasImages() {
    return $this->has('images');
}

public function scopeCompleted() {
    return $this->where('status', 'complete');
}

然后我用

Subject::completed()->hasImages()->limit(100)->get()[0]->status;

遗憾的是,结果是“待定”

只是为了弄清楚,这有效:

Subject::completed()->limit(100)->get()[0]->status;

结果是“完整”

1 个答案:

答案 0 :(得分:1)

更改 Local Scopes 以返回\Illuminate\Database\Eloquent\Builder个实例:

public function scopeHasImages($query) 
{
    return $query->has('images');
}

public function scopeCompleted($query)
{
    return $query->where('status', 'complete');
}

然后链接范围将起作用:

Subject::completed()->hasImages()->limit(100)->get();