1

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");
Josh from Qaribou
  • 6,776
  • 2
  • 23
  • 21
gucci boss
  • 11
  • 4

2 Answers2

1

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.

Daniel
  • 2,621
  • 2
  • 18
  • 34
1

Simply use RAND() to order randomly;

$products = DB::select("SELECT * FROM products where category = 75 ORDER BY RAND() LIMIT 4");
lucky
  • 12,734
  • 4
  • 24
  • 46