PHP Laravel database seeder example with some sample user data insertion

PHP Laravel database seeder example with some sample user data insertion

Before jumping to anything, the first thing that may come to your mind “what is seeder” and “how can we use it in our laravel application”. Here in this article we are going to learn PHP Laravel database seeder example with some sample user data insertion and learn how to use database seeder in laravel.

As a developer, while working on a feature of an application, sometimes we need some raw data in the database for features testing and we know data insertion in the DB manually is painful andunnecessarily time consuming task. So to overcome this problem, database seeder in used in the laravel application.

In this example tutorial we will seed user data in our db and this data is only for testing purposes.

We will generate seader class by executing following command in the terminal

php artisan make:seeder UsersTableSeeder

After successful execution of above command we will get a UserTableSeeder.php file under the directory database/seeds and a class UserTableSeeder.

In this file there are only one method defaulted that is “run()”.  In this method we will write some line of code for inserting data in the user table.

So open “UsersTableSeeder.php” file and add the following code to inder data in user table.

database/seeds/UsersTableSeeder.php

<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'first_name' => str_random(6),
            'last_name' => str_random(6),
            'address' => str_random(50),
            'email' => strtolower(str_random(6)).'@gmail.com',
            'password' => bcrypt('password')
        ]);
    }
}

Now the script is ready to insert sample data in user table and this script will insert only one row and you want to add multiple row into the table, you can use insert code into a loop like below

for($i=1;$i<=1000;$i++){
   DB::table('users')->insert([
     'first_name' => str_random(6),
     'last_name' => str_random(6),
     'address' => str_random(50),
     'email' => strtolower(str_random(6)).'@gmail.com',
     'password' => bcrypt('password')
   ]);
}

Now we will need to run this script to seed our DB table with somae raw data. So for that we will execute following command

php artisan db:seed --class=UsersTableSeeder

If we will run above command db:seed without any specified parameter. It will execute DatabaseSeeder class.

Think if we have multiple seeder class for seeding data the executing each class will be a hectic task. So to overcome this problem thereis a solution to call all the seeder class from one place. Laravel provides a call() method We can use it in run() method of  DatabaseSeeder class like below.

<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
    }
}

Now run only one command given below to seed all seeder class.

php artisan db:seed