How to Create Database Seeder in Laravel?
Today, I will let you know example of how to create seeder in laravel. I’m going to show you about laravel seeder multiple records. you can understand a concept of how to make seeder in laravel. I explained simply about how to create seeder file in laravel. follow the below example for how to create user seeder in laravel.
You have lots of questions what is seeder in laravel?, how to use seeder in laravel?, what is the command to create seeder in laravel?, why do we have to use seeder in laravel? that's all I will example in this laravel database seed tutorial.
Laravel introduce a seeder for creating testing or default data and if you have a small admin project then you can create an admin user and also set table default data.
Whenever you have an admin project that doesn't have a register page then what you will do? I mean you need to create at least one admin user. So basically he can log in and access the whole admin panel. But you don't have a register page on the front end. you only have a login page. So you can create one admin from the database directly?, if yes then you have to always create a new admin user from the directly database when you create a new setup of your project. But I will suggest you create an admin seeder so you can create an admin user using laravel seeders. You just fire on command to run seeder in laravel.
You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.
Same things, if you have a default setting configuration then you can create a setting seeder and add default configuration to your database table.
In this tutorial, I will show you how to create a database seeder in laravel and what is command to create a seeder and how to run that seeder in laravel. so you have to just follow a few steps to get how it's done.
Create Seeder Class:
Laravel gives command to create seeder in laravel. so you can run following command to make seeder in laravel application.
Create Seeder Command:
php artisan make:seeder AdminUserSeeder
after running the above command, it will create one file AdminUserSeeder.php on the seeders folder. All seed classes are stored in the database/seeders directory.
Then you can write code of create admin user using model in laravel.
class AdminUserSeeder extends Seeder
* Run the database seeds.
* @return void
public function run()
'name' => 'Hardik',
'email' => 'firstname.lastname@example.org',
'password' => bcrypt('123456'),
As you can see above code, I simply write creating a new user in my users table now. so you can add your code to write admin user creating. Now, how you can run this seeder manually.
There is a two way to run this seeder. I will give you both ways to run seeder in laravel.
Way 1: Run Single Seeder
You need to run following command to run single seeder:
php artisan db:seed --class=AdminUserSeeder
Way 2: Run All Seeders
In this way, you have to declare your seeder in the DatabaseSeeder class file. then you have to run a single command to run all listed seeder class.
So can list as bellow:
class DatabaseSeeder extends Seeder
* Seed the application's database.
* @return void
public function run()
Now you need to run following command for run all listed seeder:
php artisan db:seed
You can see one database row will created on users table:
Now I think you will understand how seeding is work and we have to use it in our laravel app.
I hope it can help you...
We are Recommending you
- How to Run All Seeders in Laravel?
- Laravel US State Seeder Example
- Laravel Seeder from CSV File Example
- How to Run Specific Seeder in Laravel?
- How to Create Seeder with JSON data in Laravel?
- How to Run Migration and Seeder on Laravel Vapor?
- How to Change Column Length using Laravel Migration?
- How to Update Enum Value in Laravel Migration?
- Laravel Migration Enum Default Value Example
- Laravel Migration Add Enum Column Example
- How to Rollback Migration in Laravel?
- Laravel Import Large SQL File using Seeder Example
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?