0

I have the code:

$products = Product::whereIn('id_principal_category', $array)->inRandomOrder()->paginate(6);

It sometimes shows me a repeated record.

I have read that I must use whereNotIn, the problem is that it can work if it just one time... but how can i do that if does it have a paginator? because i dont know which the repeated records are and i cant use whereNotIn.. so my question is how can I do that inRandomOrder does not show a repeated record with paginator?

Thanks

Jesus Cova
  • 33
  • 4

2 Answers2

0

See if unique() method helps.

$products = Product::whereIn('id_principal_category', $array)->unique()->inRandomOrder()->paginate(6);
Sami Samiuddin
  • 438
  • 4
  • 8
0

You can't use random ordering with pagination ,mysql cannot track the rows for each visitor . I quoted those lines from Chris Henry's answer

Solution 1

Pick from a number of existing columns that already indexed for being sorted on. This can include created on, modified timestamps.

Solution 2

You could have an additional column that stores a random number for sorting. It should be indexed, obviously. Periodically, run the following query;

UPDATE table SET rand_col = RAND();
Ahed Eid
  • 395
  • 4
  • 17