Laravel 8 Run Specific Seeder Example

In this article, we will explore how to run a specific seeder in Laravel 8, 9, or 10. If you ever need to execute a single seeder in any of these Laravel versions, you can use the process to run a specific seeder we are about to discuss. We'll guide you through the steps to run a particular seeder in Laravel, effectively populating your database with specific data.

Laravel provides a powerful feature for seeding databases using seed classes, with all such classes stored in the database/seeders directory. Additionally, any seeders generated by the framework are conveniently placed in the same directory.

Let's dive into the details of running a specific seeder in Laravel 8, 9, or 10. We will utilize the db:seed Artisan command and the --class option to specify and execute individual seeders.

Run Specific Seeder in Laravel

Below, I've provided a set of steps to run a specific seeder in Laravel 8,9 or 10. You can utilize the --class option to specify and run a particular seeder class individually

php artisan db:seed --class=AdminSeeder

Now, you will find the seeder file in this file location database/seeders/AdminSeeder.php.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Admin;


class AdminSeeder extends Seeder

{
/**
* Run the database seeds.
*
* @return void
*/

    public function run()
    {

        Admin::create([

            "name" => "admin",
            "email" => "admin@gmail.com",
            "password" => bcrypt("12345")

        ]);

    }

}

 

You can run all the seeders in Laravel using the following command: db:seed. This Artisan command is used to seed your database. By default, the db:seed command runs the Database\Seeders\DatabaseSeeder class, which may, in turn, trigger the execution of other seed classes.

php artisan db:seed

 

You can run migrations along with seeders in Laravel using the following command: migrate:fresh --seed. This command not only performs database migrations but also seeds your database. It's particularly useful for completely rebuilding your database, as it drops all tables and then reruns all migrations, ensuring that the seed data is also applied

php artisan migrate:fresh --seed

 

You can run seeders for production in Laravel using the following command: --force. To execute the seeders without any user prompts, simply include the --force flag. This is a handy way to run seeders in production environments.

php artisan db:seed --force

//To run spicific seeder

php artisan db:seed --class=AdminSeeder --force

 

Conclusion:

In conclusion, we've explored the process of running a specific seeder in Laravel 8, 9, or 10. Whether you're working with one of these Laravel versions, the ability to execute individual seeders is a valuable feature for populating your database with precise data. We've also touched on running all seeders, migrating with seeders, and forcing seeders to run in production environments.

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS