举例来说,假设我有以下内容,其中为每个拥有一顶帽子的人建立了关系。
$people = People::join('hat')->get();
Run Code Online (Sandbox Code Playgroud)
我如何过滤我的结果,只给我那些帽子是红色的人?
我试过了,
$people = People::join('hat')
->where('hat.colour', 'red')
->get();
Run Code Online (Sandbox Code Playgroud)
...但到目前为止还没有运气。
干杯
使用whereHas():
$people = People::whereHas('hat', function($query) {
$query->where('colour', 'red');
})->get();
Run Code Online (Sandbox Code Playgroud)