The Complete Guide to Database Seeder in Laravel 6

The Complete Guide to Database Seeder in Laravel 6

You have lots of question what is seeder in Laravel 6? How to use seeder in Laravel 6? What is command to create seeder in Laravel 6? Why we have to use seeder in Laravel 6? That's all I will example in this Laravel 6 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 6 seeder. You just fire on command to run seeder in Laravel 6.

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 6 and what is command to create seeder and how to run that seeder in Laravel 6. 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' => '[email protected]',

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

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

laravel php database web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...