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");
Simply use RAND()
to order randomly;
$products = DB::select("SELECT * FROM products where category = 75 ORDER BY RAND() 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.
category = 75
looks like it should be in awhere
clause.category = 75
is misleading, if you really want random categories.