Laravel 7 Database Seeder Tutorial

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish but probably should follow some sensible convention, such as UserSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

So let’s start our tutorial


Now Open your terminal and put the below command:

php artisan make:seeder AdminRoleSeeder

After run Successfully run the command it will store in database/seeds directory.

So open this file name is AdminRoleSeeder.php put the below code.

<?php

use Illuminate\Database\Seeder;
use App\User;

class AdminRoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(){
        User::create([
            'name' => 'Dhaval',
            'email' => 'dhaval.shah1095@gmail.com',
            'password' => bcrypt('dha@1095'),
            'role' => "Admin",
        ]);
    }
}

Now we have to run the seeder

So open your terminal and put the below code.

php artisan db:seed --class=AdminRoleSeeder

I hope these will help you!

About Dhaval Shah

My name is Dhaval Shah. I'm a PHP developer, entrepreneur and CEO of DAG inventions PVT ltd. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!