Laravel 8 Database Seeder Example

Laravel , Laravel 8

Nicesnippets

175

15-09-2020


Hello Friends,

In this blog, I will show you how to create database seeder in laravel 8 application. I will let you know example of laravel 8 seeder example. As an example, let's modify the default DatabaseSeeder class and add a database insert statement to the run method.

Laravel provides a tool to add sample or dummy data to our databases automatically. that is call it database seeding.

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 8 seeder. You just fire on command to run seeder in laravel 8.

In this tutorial, i will show you how to create database seeder in laravel 8 and what is command to create seeder and how to run that seeder in laravel 8. 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 AdminSeeder

after run above command, it will create one file AdminSeeder.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 8.

database/seeds/AdminSeeder.php

<?php

use Illuminate\Database\Seeder;

use App\Models\User;

class AdminSeeder extends Seeder

{

/**

* Run the database seeds.

*

* @return void

*/

public function run()

{

User::create([

'name' => 'Admin',

'email' => 'admin@admin.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 8.

Way 1 : Single Seeder Run

You can run only single perticuler then using bellow command Here i will run AdminSeeder:

php artisan db:seed --class=AdminSeeder

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(AdminSeeder::class);

}

}

Now you need to run following command for run all listed seeder:

php artisan db:seed --class=DatabaseSeeder

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...


Recommended Posts