How to Create Database Seeder in Laravel 11?

20-Mar-2024

.

Admin

How to Create Database Seeder in Laravel 11?

Hi, Dev

In this tutorial, I'll guide you through the process of crafting a database seeder within a Laravel 11 application.

Firstly, let's clarify the concept of a seeder in Laravel 11. Essentially, a seeder serves as a mechanism for generating initial or test data within your Laravel application. It's particularly useful for scenarios where you need to populate your database tables with predefined data.

Now, you might be wondering: How exactly do we utilize a seeder in Laravel 11? What commands are involved in creating and executing seeders? And why are seeders necessary within the Laravel 11 framework? I'll address each of these queries comprehensively as we delve into this tutorial.

Laravel conveniently offers seeders as a means to streamline the process of setting up default or test data. For instance, in a small-scale admin project, you might require an initial admin user along with some default data in various tables. Instead of manually inserting this data each time, you can utilize seeders to automate the process.

Consider the scenario where your admin project lacks a registration page. In such a case, creating an admin user directly from the database seems like a plausible solution. However, it's not the most efficient approach, as it mandates manual intervention every time you initialize your project. A more elegant solution lies in crafting an admin seeder. By employing Laravel 11 seeders, you can effortlessly generate an admin user with just a single command invocation.

Moreover, if your application entails default configuration settings, employing a setting seeder proves advantageous. This allows you to predefine and insert default configurations into your database tables seamlessly.

In essence, leveraging Laravel 11 seeders empowers you to automate the process of seeding your database with initial or test data, enhancing efficiency and streamlining the development workflow.

Create Seeder Class:


Laravel provides a command to create a seeder in Laravel. So, you can run the following command to make a seeder in your Laravel application:

Create Seeder Command:

php artisan make:seeder AdminUserSeeder

After running the above command, it will create one file `AdminUserSeeder.php` in the `seeders` folder. All seed classes are stored in the `database/seeders` directory.

Then, you can write the code to create an admin user using a model in Laravel.

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' => 'Piyush Kamani',

'email' => 'pkamani13@gmail.com',

'password' => bcrypt('123456'),

]);

}

}

As you can see in the code above, I've simply written the process for creating a new user in my users table. Now, you can add your code to create an admin user.

Here are two ways to manually run this seeder in Laravel 11:

Example 1: Run Single Seeder

You need to run the following command to run a single seeder:

php artisan db:seed --class=AdminUserSeeder

Example 2: Run All Seeders

In this way, you have to declare your seeder in the DatabaseSeeder class file. Then you have to run a single command to run all listed seeder classes.

So it can be listed as below:

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);

}

}

Now you need to run the following command to execute all the listed seeders:

php artisan db:seed

You can see that one database row will be created in the users' table.

laravel-11-db-seed

Now, I think you will understand how seeding works, and we have to use it in our Laravel app.

I hope it can help you...

#Laravel 11