Laravel 10 Database Seeder Example Tutorial
Hey Guys,
Here, I will show you how to create seeder in laravel 10. you can understand a concept of laravel 10 seeder example. you will learn laravel 10 database seeder example. you can understand a concept of laravel 10 database seeder.
You have lots of questions what is seeder in laravel 10?, how to use seeder in laravel 10?, what is the command to create seeder in laravel 10?, why do we have to use seeder in laravel 10? that's all I will example in this laravel 10 database seed tutorial.
Laravel introduces 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 10 seeders. You just fire on command to run seeder in laravel 10.
Same thing, if you have a default setting configuration then you can create a setting seeder and add the default configuration to your database table.
In this tutorial, I will show you how to create a database seeder in laravel 10 and what is command to create a seeder and how to run that seeder in laravel 10. 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.
database/seeders/AdminUserSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
class AdminUserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::create([
'name' => 'Hardik',
'email' => 'admin@gmail.com',
'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 10.
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:
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call(AdminUserSeeder::class);
}
}
Now you need to run following command for run all listed seeder:
php artisan db:seed
You can see one database row will be created on the user's table:
Now I think you will understand how seeding works and we have to use it in our laravel app.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Eloquent Mutators and Accessors Example
- Laravel 10 Authentication using Jetstream Tutorial
- Laravel 10 Import Export Excel and CSV File Tutorial
- Laravel 10 Auth with Inertia JS Jetstream Example
- Laravel 10 Authentication using Breeze Tutorial
- How to Send Email using Gmail in Laravel 10?
- Laravel 10 Bootstrap Auth Scaffolding Tutorial
- Laravel 10 Vue JS Auth Scaffolding with Vite Tutorial
- Laravel 10 Generate PDF File using DomPDF Example
- Laravel 10 Multiple File Upload Tutorial Example
- Laravel 10 Multiple Image Upload Tutorial Example
- Laravel 10 File Upload Example Tutorial
- Laravel 10 Form Validation Tutorial Example
- Laravel 10 Image Upload Example Tutorial
- Laravel 10 CRUD Application Example Tutorial