【发布时间】:2019-07-22 12:39:06
【问题描述】:
我想把下面的查询变成 Laravel Eloquent。
select * from tool where id not in (select tool_id from product_tool where product_id = 1)
到目前为止,我已经尝试了以下方法。
$cari = Product::where('slug', $slug)->first();
if (empty($cari)) {
abort(404);
}
$productTool = ProductTool::where('product_id', $cari->id)->get();
foreach ($productTool as $data) {
$tools = Tool::whereNotIn('id', [$data->tool_id])->get();
}
问题是当我 dd() $tools 时,它只返回一个数组。它需要返回两个数组,因为在 product_tool 表中(与 products 和 tools 表是多对多的)product_id 1 有 tool_id 的 id [1,2]。
【问题讨论】: