I have a request for a job in phpMyAdmin
SELECT DISTINCT id, name, articul FROM products WHERE category_id=40 order by articul;
How do I perform this request in the correct format using the query builder in Laravel? What should the model's approach be?
First, I tried using the following code:
$products = Product::orderBy('aerucul') ->select('articul', 'id', 'name') ->distinct() ->where('category_id', 40) ->get();
But I get duplicate results.
If I try to use the following code, the result is the same:
$products = DB::select("SELECT DISTINCT id, name, articul FROM products WHERE category_id='40' order by articul");
Try to use
DB::table()
methodIf you only want to get a product, instead of using
->get()
, usefirst()