Laravel Database Seeder

During development, we are often needed to insert some dummy data into our database (for testing purposes).

This step is important, especially when you are building a system to handle million of data.

Thus, you will need to have a million of data in your database for testing purposes.

In Laravel, we can use Database Seeder to insert dummy data into our database.

In this post, I will share on how to insert 50,000 dummy data into our database.

Step 1:

Run this command to create a Seeder class:

php artisan make:seeder SimSeeder

You can replace "SimSeeder" with whatever name you like.

This will create a new file named "SimSeeder.php" located at "database/seeders/SimSeeder.php"

Step 2:

Edit the file SimSeeder.php with code below:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Sim;

class SimSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$data = [];

// SIM Seeder
for ($i=0; $i<50000; $i++){
$data[] = [
'iccid' => "89".rand(1,9999999999999999),
'imsi' => "51".rand(1,9999999999999),
'puk1' => rand(1,999).rand(1,999),
'puk2' => rand(1,999).rand(1,999),
'idsim_types' => rand(1,2),
'idsim_statuses' => rand(1,2),
];
}

$chunks = array_chunk($data, 5000);
foreach($chunks as $chunk){
Sim::insert($chunk);
}
}
}

Step 3:

In your terminal, run this command:

php artisan db:seed --class=SimSeeder

This will run the seeder, and 50,000 dummy data will be inserted into our database.