Redgren Grumbholdt
Redgren Grumbholdt

Reputation: 1230

Implementation of where clause in Laravel find()

Here is my code:

public function order($id)
{
    $product = Product::find($id);
    $other_products = Product::find($id)->toArray();
    return view('products.order',['product'=>$product,'other'=>$other_products]);
}

So my question is how can i exclude $product from the $other_products query, more like SELECT * FROM table WHERE product != $id

Upvotes: 1

Views: 854

Answers (3)

dev_a.y
dev_a.y

Reputation: 133

Try:

$other_products = Product::where('id', '!=', $id)

OR

$other_products = Product::whereNotIn('id', [$id])

OR

$other_products = Product::where('id', '<>', $id)

Upvotes: 3

S&#233;rgio Reis
S&#233;rgio Reis

Reputation: 2523

$other_products = Product::where('id','!=',$id)->get();

Check https://laravel.com/docs/5.5/queries#where-clauses you can build queries using elequent models

Upvotes: 0

Sohel0415
Sohel0415

Reputation: 9853

Use where()

$other_products = Product::where('id', '!=', $id)->get()->toArray();

Upvotes: 2

Related Questions