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,516
  • 2
  • 22
  • 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,263
  • 2
  • 16
  • 32
1

Simply use RAND() to order randomly;

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