Random search

Asked

Viewed 172 times

1

I am making a random query in the table users of my application in laravel, example below:

User::inRandomOrder()->paginate(10)

It actually returns me the result randomly, but I need the entire record of the table users with the column users_destaque = 1 be listed first randomly, and every record with the column users_destaque = 0 be listed later and also randomly, I stand by.

1 answer

1


You must also place your ordination in this case ...('users_destaque', 'DESC').

Try the following, Laravel versions < 5.2:

User::orderBy('users_destaque', 'DESC')->orderByRaw("RAND()")->paginate(10);

For a version of Laravel >= 5.2 is as you were doing, with addition of main ordering:

User::orderBy('users_destaque', 'DESC')->inRandomOrder()->paginate(10);
  • I’m finalizing the Laravel 5.4, I try this code same ?

  • Hello @Brunosantos, in this case is the second example you should use

Browser other questions tagged

You are not signed in. Login or sign up in order to post.