我需要获取那些没有加入的记录有很多表 我的查询是这样的
$result = Recipes::select('id')->where("name", 'like', '%' . $item . '%')
->with(['categoryItem'])
->get();
我的输出是
Array
(
[0] => Array
(
[id] => 1
[category_item] => Array
(
[0] => Array
(
[id] => 2
[foodable_id] => 1
[foodable_type] => Recipes
[created_at] => 2017-04-14 16:18:22
)
)
)
[1] => Array
(
[id] => 4
[category_item] => Array
(
)
)
)
我只想获取那些 category_item 详细信息为空的记录。在下面的示例中,我想获取索引 1 的记录,那么我如何通过查询给出条件
最佳答案
如果我对你的理解是正确的,并且你想获得没有categoryItem
的收件人,你想使用doesntHave()
方法:
Recipes::where('name', 'like', '%'.$item.'%')
->doesntHave('categoryItem')
->get();
我假设 categoryItem
是已定义的关系,因为您正试图在 with()
方法中使用它。
关于php - 如何在 Eloquent laravel中检查hasMany关系记录为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468084/