在Laravel中实现where子句find()

时间:2018-04-19 08:43:16

标签: php laravel eloquent

这是我的代码:

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

3 个答案:

答案 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模型构建查询