我有这样的事情:
$items = Item::whereIn("name", $request["names"])->get();现在我想要一张有这些商品的商店的清单。就像这样:
$shops = Shop::whereIn("item_id", $items)->get();这是行不通的,因为项目是一个雄辩的集合(显然)。我可以做一些循环,得到如下所有I:
foreach ($items as $item){
$itemIds[] = $item
}然后使用它,但我觉得一定有一个更干净的方法。
有什么帮助吗?
发布于 2022-04-20 14:03:36
使用->pluck('id')和whereIn()
$items = Item::where('name', $request['names'])->get();
$shops = Shop::whereIn('item_id', $items->pluck('id'))->get();雄辩的收藏品可以使用Laravel收藏类的许多相同功能:
https://laravel.com/docs/9.x/collections#available-methods
->pluck('id')将返回所有items.id记录的数组,然后您可以使用该数组通过->whereIn('item_id', ...);进行查询。
https://stackoverflow.com/questions/71941048
复制相似问题