关系雄辩的约束

时间:2016-08-02 08:27:03

标签: laravel laravel-5 eloquent laravel-5.2

我的数据如下:$completes = Complete::with(['answers', 'survey.location'])->orderBy('created_at', 'DESC')->get();

如何将所有completes放在survey.location = 1

我尝试过像Location::where()->with('completes')之类的操作,但我需要completes订购,而不是按location分组

1 个答案:

答案 0 :(得分:2)

试试这个:

$completes = Complete::with('answers')
                       ->whereHas('survey', function ($query) {
                             $query->where('location','=',1);
                        })
                       ->orderBy('created_at', 'DESC')->get();

有关详细信息,请查看Constraining Eager Loads部分中的Doc:)