标题很难说,但我正在寻找的是我正在执行的Tank::with('oil', 'site')
,然后需要执行->where(site.location, 'LIKE', 'mylocation')->get()
,但site.location并没有从with导入的带有check的站点中获取任何内容。我将如何去做。
并且如果有人可以用更好的方法来写标题,请说出来,以便如果它得到解决,其他人会发现它更容易。
谢谢你,乔丹。
与示例一起使用
Tank::whereHas('site', function (Builder $query) {
$query->where('location', 'like', 'mylocation%');
})->get();
您可以尝试以下方法:
$result = Tank::with(['oil', 'site' => function($q) use($value) {
$q->where('location', 'LIKE', $value);
}])->get();
也可以尝试使用此方法(只有查询返回时,它才会获取记录您想要的位置,否则为无):
$result = Tank::with('oil', 'site')->whereHas('site', function($q){
$q->where('location', 'LIKE', 'mylocation');
})->get();