这是我的代码:
public function order($id)
{
$product = Product::find($id);
$other_products = Product::find($id)->toArray();
return view('products.order',['product'=>$product,'other'=>$other_products]);
}
所以我的问题是如何从$product
查询中排除$other_products
,更像是SELECT * FROM table WHERE product != $id
答案 0 :(得分:3)
尝试:
$other_products = Product::where('id', '!=', $id)
OR
$other_products = Product::whereNotIn('id', [$id])
OR
$other_products = Product::where('id', '<>', $id)
答案 1 :(得分:2)
使用where()
$other_products = Product::where('id', '!=', $id)->get()->toArray();
答案 2 :(得分:0)
$other_products = Product::where('id','!=',$id)->get();
检查https://laravel.com/docs/5.5/queries#where-clauses您可以使用elequent模型构建查询