This is my line of code that shown products from database, but I want to show it randomly every time.
Code:
$products = DB::select("SELECT * FROM products ORDER BY category = 75 DESC LIMIT 4");
This is my line of code that shown products from database, but I want to show it randomly every time.
Code:
$products = DB::select("SELECT * FROM products ORDER BY category = 75 DESC LIMIT 4");
Why you ordering by category = 75? You meant where category = 75
?
You can use ORDER BY RAND()
:
$products = DB::select("SELECT * FROM products ORDER BY RAND() LIMIT 4");
but you may expect some performance issues.
Simply use RAND()
to order randomly;
$products = DB::select("SELECT * FROM products where category = 75 ORDER BY RAND() LIMIT 4");