How To Fetch Resources Randomly in Laravel with a Single Line Of Code.

Have you encountered a challenge where you need to fetch records from the database in no particular order using Laravel?

I found myself at this point once and here is how I solved it. All thanks to dear Google!?

This is a regular SQL query to fetch r…

Have you encountered a challenge where you need to fetch records from the database in no particular order using Laravel?

I found myself at this point once and here is how I solved it. All thanks to dear Google!?

This is a regular SQL query to fetch records randomly:

select * from `users` order by RAND() ```



But in Laravel, there is a method named

 ```inRandomOrder()```

 which simply fetches the records in random order.

Here is a list of the few ways this method can be used:




```PHP

//Fetch all records in random order
Model::inRandomOrder()->get();

// 5 indicates the number of records
Model::inRandomOrder()->limit(5)->get();

//Fetc all records in pages or sets of 6
Model::inRandomOrder()->paginate(6)->get();

// get one random record
Model::inRandomOrder()->first()

The helper method can also be used like this:

Model::where('name',$name)->inRandomOrder()->get();

PS: The inRandomOrder() method can also be used alongside other helper methods for fetching records like paginate() and limit().



Thats It!?

I trust this will be of great assistance?. I would also love to learn more about this so please feel free to drop your comments or send a direct message on twitter.
Questions are welcomed. Don’t forget to connect with me on socials.?

Thank you?


Print Share Comment Cite Upload Translate
APA
Funke Olasupo | Sciencx (2024-03-29T10:51:02+00:00) » How To Fetch Resources Randomly in Laravel with a Single Line Of Code.. Retrieved from https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/.
MLA
" » How To Fetch Resources Randomly in Laravel with a Single Line Of Code.." Funke Olasupo | Sciencx - Monday July 5, 2021, https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/
HARVARD
Funke Olasupo | Sciencx Monday July 5, 2021 » How To Fetch Resources Randomly in Laravel with a Single Line Of Code.., viewed 2024-03-29T10:51:02+00:00,<https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/>
VANCOUVER
Funke Olasupo | Sciencx - » How To Fetch Resources Randomly in Laravel with a Single Line Of Code.. [Internet]. [Accessed 2024-03-29T10:51:02+00:00]. Available from: https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/
CHICAGO
" » How To Fetch Resources Randomly in Laravel with a Single Line Of Code.." Funke Olasupo | Sciencx - Accessed 2024-03-29T10:51:02+00:00. https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/
IEEE
" » How To Fetch Resources Randomly in Laravel with a Single Line Of Code.." Funke Olasupo | Sciencx [Online]. Available: https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/. [Accessed: 2024-03-29T10:51:02+00:00]
rf:citation
» How To Fetch Resources Randomly in Laravel with a Single Line Of Code. | Funke Olasupo | Sciencx | https://www.scien.cx/2021/07/05/how-to-fetch-resources-randomly-in-laravel-with-a-single-line-of-code/ | 2024-03-29T10:51:02+00:00
https://github.com/addpipe/simple-recorderjs-demo