Sometimes we want to show random data from a table. Eg.
I’m going to share a few good ways, that I know. But there could be many other better ways to do it.
Laravel >= 5.2 have inRandomOrder() method to take random records from a database table, and it’s quite simple.
$numbers = DB::table('numbers')->inRandomOrder()->take(10)->get();
// OR using Model
$numbers = Number::inRandomOrder()->take(10)->get();
But, the bad thing about this method is, if you’ve got a large table, eg. 500K or 1M or more rows, it will take a lot of time to retrieve the records from database table compared to other methods that I’ll show below.
First, you run a query to take all values of the primary key column. Then, take 10 random values from them. Finally, run another query to take rows from the table using the 10 random values as whereIn() values. This method would be a bit faster than the previous method.
$numbers = DB::table('numbers')->get('id')->random(10);
$numbers_id = Arr::pluck($numbers, 'id');
$numbers = DB::table('numbers')->whereIn('id', $numbers_id)->get();
// OR using Model
$numbers = Number::get('id')->random(10);
$numbers_id = Arr::pluck($numbers, 'id');
$numbers = Number::whereIn('id', $numbers_id)->get();
If you have a large table, you can take recent 1000 (any good large amount) rows using the take() method, then take 10 random rows using the random() method. Using this method, you’ll get the random rows faster.
$numbers = DB::table('numbers')->take(1000)->get()->random(10);
// OR using Model
$numbers = Number::take(1000)->get()->random(10);
Create an additional column, name it something like – ‘sorter’. Create a CRON Job or Scheduled Task to set/update this column’s values between 1-100 integers every few hours. Then when you run a query, sort the query by that column. This method could be faster, but a bit slower than the previous method to take random records from a table.
$numbers = DB::table('numbers')->orderBy('sorter')->take(10)->get();
// OR using Model
$numbers = Number::orderBy('sorter')->take(10)->get();
To test the above methods, I’ve used a table named “numbers” with 1M phone numbers in it. So, If you have a table with such an amount of data, use the right method to get random data.
Today I will share a snippet I've used in a project. Using that function, you…
JavaScript AJAX (Asynchronous JavaScript and XML) is a technique that gives the ability to send…
By default, Bootstrap carousel has no way to add animations to carousel elements. Here I'm…
Laravel comes up with a paginator that generates HTML compatible with the Tailwind CSS framework.…
Bootstrap introduced their icons collection in November 2019 as Bootstrap Icons (v1.0.0-alpha). At that time,…
To create autocomplete feature for input field(s), HTML's datalist tag can be the easiest solution.…
View Comments
This realy helped me