Laravel 11 Database Seeder Example Tutorial

  Mar 2024
  ITSolutionsGuides
  Category: Laravel
Laravel 11 Database Seeder Example Tutorial

Welcome To ITSolutionsGuides,


In this Laravel 11 Database Seeder Example Tutorial we will see how to create a database seeder and run the seeder file with a example. In this example we will create the new user details and add to the database using the seeder file. Basically seeder are used to add the user details for the inirtial run of the application without any user registration page ie; for a single user admin panel user registration page is not required therefore to add the user details seeders where used. In this Laravel 11 database seeder tutorial, learn how to efficiently populate your database with dummy data using Laravel's powerful seeding feature. Explore step-by-step examples to streamline your development process and ensure smooth database operations. Laravel's latest version introduces a handy seeder for generating default or testing data. In scenarios like a small admin project lacking a registration page, setting up an initial admin user solely through the database becomes essential. While feasible, this method requires manual admin user creation each time the project is set up. To streamline this process, I propose leveraging Laravel 11's seeder functionality to create an admin seeder. With a single command execution, you can conveniently generate an admin user, enhancing project setup efficiency.

Let's Create Seeder

Lets create the seeder file using the artisan command,
php artisan make:seeder AdminUserSeeder
the above command will create the AdminUserSeeder.php file in the database/seeders/ directory , Lets edit the seeder file to create the new user.


database/seeders/AdminUserSeeder.php

<?php
  
namespace Database\Seeders;
  
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
  
class AdminUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        User::create([
            'name' => 'ITSolutionsGuides',
            'email' => 'itsolutionsguides@gmail.com',
            'password' => bcrypt('123456'),
        ]);
    }
}

Let's Run Seeder

Lets run the AdminUserSeeder using the following artisan command using the --class flag we can run a specific seeder file

php artisan db:seed --class=AdminUserSeeder

Let's Run Seeder

Lets register the AdminUserSeeder in the DatabaseSeeder.php file,


database/seeders/DatabaseSeeder.php

<?php
  
namespace Database\Seeders;
  
use Illuminate\Database\Seeder;
  
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->call(AdminUserSeeder::class);
    }
}

Let's Run Seeder

after registering the AdminUserSeeder in the DatabaseSeeder.php , Lets execute the below artisan command which will execute all the seeder files registered in the DatabaseSeeder.php.

php artisan db:seed

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides