Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AhmadYousef's avatar

inRandomOrder with paginate returns duplicated records

Hi, I want to show the data for one of my models in a random order, the easiest way was to write it like this:

$products = Product::where('id',$id)->inRandomOrder()->paginate(10);

Now, this work perfectly for the first page, but sometimes I see the same data in other pages. Is there a better way to get the desired result without duplicates ?

0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Try adding a seed

$products = Product::where('id',$id)->inRandomOrder('foobar')->paginate(10)
3 likes
masiseng7's avatar

I'm sorry, what the meaning is 'foobar' ? thx

stuartcusack's avatar

@Elias Fonseca If you use a table column here then the records will never be randomised - always the same results. This is what I ended up doing, perhaps not the best way, but here it is:

// Seed should be integer (although table column is accepted for some reason??)
if(!session()->exists('table_order')) {
  session()->put('table_order', rand(0, 999999));
}

$seed = session('table_order');

...
->inRandomOrder($seed)
AhmadYousef's avatar

That did it ! Thank you !

Why isn't that in the documentation tho?

Please or to participate in this conversation.