Laravel 7 Database Seeder Example
Today, i will let you know example of laravel 7 seeder example. In this article, we will implement a laravel 7 seeder generator. you can understand a concept of create seeder in laravel 7. you can see create seeder command in laravel 7. You just need to some step to done laravel 7 seeding tutorial.
You have lots of question what is seeder in laravel 7?, how to use seeder in laravel 7?, what is command to create seeder in laravel 7?, why we have to use seeder in laravel 7? that's all i will example in this laravel 7 database seed tutorial.
Laravel introduce seeder for creating testing data and if you have small admin project then you can create admin user and also setting table default data.
Whenever you have admin project that don't have register page then what you will do?, i mean you need to create at least one admin user. So basically he can login and access whole admin panel. But you don't have register page on front end. you only have login page. So you can create one admin from database directly?, if yes then you have to always create new admin user from directly database when you create new setup of your project. But i will suggest you to create admin seeder so you can create admin user using laravel 7 seeder. You just fire on command to run seeder in laravel 7.
Same things, if you have default setting configuration then you can create setting seeder and add default configuration to your database table.
In this tutorial, i will show you how to create database seeder in laravel 7 and what is command to create seeder and how to run that seeder in laravel 7. so you have to just follow few step get how it's done.
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 run above command, it will create one file AdminUserSeeder.php on seeds folder. All seed classes are stored in the database/seeds directory.
Then you can write code of create admin user using model in laravel.
database/seeds/AdminUserSeeder.php
<?php
use Illuminate\Database\Seeder;
use App\User;
class AdminUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
User::create([
'name' => 'Hardik',
'email' => 'admin@gmail.com',
'password' => bcrypt('123456'),
]);
}
}
As you can see above code, i simply write creating 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 way to run seeder in laravel 7.
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 DatabaseSeeder class file. then you have to run single command to run all listed seeder class.
So can list as bellow:
database/seeds/DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(AdminUserSeeder::class);
}
}
Now you need to run following command for run all listed seeder:
php artisan db:seed
Now i think you will understand how seeding is work and we have to use 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
- How to Make PDF File in Laravel 7?
- Laravel 7 Auth Example | Laravel 7 Authentication Example
- How to Create Custom Helper Function in Laravel 7?
- Laravel 7 Ajax Image Upload Tutorial
- Laravel 7 Ajax Form Validation Example
- Laravel 7 File Upload Example
- How to install Bootstrap 4 in Laravel 7?
- Laravel 7 CRUD Example | Laravel 7 Tutorial For Beginners