How To Create Database Seeder In Laravel 10

  Aug 2023
  ITSolutionsGuides
  Category: Laravel
How To Create Database Seeder In Laravel 10

Hi Developers,

Lets see how to create a database seeder in laravel 10. It is more use full in Admin related projects where the register page is not required but still we need admin details for the login the how its done . In this case we can use laravel Seeders for adding data in the database without any register page to add details . Lets see how its done.

All the Seeder files generated in laravel framwork are under the database/seeders folder . By default Seeder class contains only the run() method and when the Seeder class is called the run() method will be executed. We can use Query builders or Eloquent model factories to store data in database through the run() method.

Let's Create a Seeder

php artisan make:seeder AdminSeeder

Create AdminSeeder using above command and edit the file

database/seeders/AdminUserSeeder.php

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

Run The Seeder

Lets Run the AdminSeeder By using the following command

php artisan db:seed --class=AdminSeeder

Adding Seeder in DatabaseSeeder

Let's Register the AdminSeeder in the DatabaseSeeder

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

Running All Seeder

The Following Command will run all the Seeder files registered in DatabaseSeeder

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