How to create laravel seeder?


Install fresh application of laravel.

composer create-project --prefer-dist laravel/laravel your_project_directory_name

Create Seeder by using below command.

php artisan make:seeder UserSeeder

UserSeeder.php File will be create inside the database/seeds folder. After creating seeders file you have to run below command.

composer dump-autoload
//database/seeds/UserSeeder.php

use Illuminate\Database\Seeder;
use App\User;
use Illuminate\Support\Facades\Hash;
  
class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        	User::create([
	            'name' => "Miss Annabelle O'Keefe I",
	            'email' => 'asha49@example.net',
	            'password' =>Hash::make(111)
	        ]);
	    	User::create([
	            'name' => 'Arno Huels',
	            'email' =>'leonardo.okon@example.net',
	            'password' =>Hash::make(111),
	        ]);
    }
}

If you want to run specific seeders for that you have to run the below command for insert record in database users table.

php artisan db:seed --class=UserSeeder

And if you have created multiple seeders in your application and you want to execute all seeders at the same time. For that you have to register your all seeders in database\seeds\DatabaseSeeder.php file like below.

//database\seeds\DatabaseSeeder.php

use Illuminate\Database\Seeder;

    class DatabaseSeeder extends Seeder{
        /**
        * Seed the application's database.
        *
        * @return void
        */
        public function run()
        {
           $this->call(UsersTableSeeder::class);

           $this->call(RoleTableSeeder::class); // second seeder
        }
    }

When you completely register you all seeder in DatabaseSeeder.php file after that run below command in your terminal.

php artisan db:seed

Now you are ready to check your database table. The record has been inserting by using laravel Seeders.

I hope you this article will helpfull for everyone.