How to create database seeder in laravel 8 ?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hello  Dev’s,
Today in this article,I will teach you how to create seeder and insert data into database in laravel 8 application. Laravel 8 includes a very simple method of seeding in our database with test data by using seed classes. And also all seed classes are stored in the location database/seeds directory folder. I will show you How to create database seeder in laravel 8.

Now in this tutorial, I will show you for know what is database seeder, and also what is the main use of database seeder and how can we generate seeder file with migrations in any Laravel application.

So now in this example, I need to seed some test/dummy data into our users table that will not created by any users in our application.

For testing our any applications where we will need different user’s with credentials to test all the scenarios, then we can populate the data by using seed functionality provided in Laravel application.

We can easily generate the seeder class by using following command on our project:

php artisan make:seeder UsersTableSeeder

So when command will execute successfully then we will get our new “UsersTableSeeder” class in database/seeds directory location. All the seeders class generated by artisan command will be place in the database/seeds directory.

So there will be only one method in seeder class by default :run.

Within by the run method, we will write the insert query to load some user data into our users table.

So good let’s open the “UsersTableSeeder.php” file and add the following line of code for insert new user data:

UsersTableSeeder

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

The above code will insert just one record at a time, If we want to insert in bulk then put the code into a loop.

Please now just run the following command to seed the data :

php artisan db:seed --class=UsersTableSeeder

When we run the artisan command with db:seed option without specifying any class, then it will run the DatabaseSeeder class.

So now we can break our seeders into multiple files and we can call them in a default “DatabaseSeeder” class by using call by method. So call method is used to call additional seed class in our application.

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

Read Also : Laravel force download file with header response

Thanks for read. I hope it help you. For more you can follow us on facebook

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →