How To Run Specific Seeders In Laravel

Laravel, one of the most popular PHP frameworks, provides a robust and elegant way to seed your database with sample data. Seeders are essential for populating your database tables with initial or test data. Sometimes, you may want to run a specific seeder to insert only certain records into your database. In this blog, we'll explore how to run a specific seeder in Laravel, along with practical examples for each step.

Understanding Seeders in Laravel

Before we dive into running specific seeders, let's understand the basics of seeders in Laravel. Seeders are classes in Laravel that allow you to populate database tables with sample data. These classes are especially useful for testing your application, sharing demo data with others, and initializing your database with necessary records.


Laravel provides a convenient Artisan command to run seeders:

php artisan db:seed


By default, this command runs all seeders specified in the DatabaseSeeder class. However, you might want to run a specific seeder to populate only certain tables. Let's see how you can achieve this.

Running a Specific Seeder in Laravel

To run a specific seeder in Laravel, you can use the --class option with the db:seed Artisan command. This option allows you to specify the class name of the seeder you want to run. Here's a step-by-step guide with examples:

Create a Seeder

First, create a seeder class if you don't already have one. You can use the make:seeder Artisan command to generate a new seeder class:

php artisan make:seeder UsersTableSeeder


This will generate a UsersTableSeeder class in the database/seeders directory.

Define Seeder Logic

Inside your seeder class, define the logic to insert the desired records into your database table. For instance, let's insert a few sample users into the users table:

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->insert([
            ['name' => 'John Doe', 'email' => 'johndoe@example.com'],
            ['name' => 'Jane Smith', 'email' => 'janesmith@example.com'],
        ]);
    }
}

Run the Specific Seeder

Now, you can run this specific seeder using the db:seed Artisan command with the --class option:

php artisan db:seed --class=UsersTableSeeder


This command will execute only the UsersTableSeeder class and insert the defined records into the users table.

Practical Example

Let's say you want to seed a categories table with sample data. First, create a seeder for this table:

php artisan make:seeder CategoriesTableSeeder


Inside the CategoriesTableSeeder class, insert sample categories:

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class CategoriesTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('categories')->insert([
            ['name' => 'Technology'],
            ['name' => 'Fashion'],
            ['name' => 'Food'],
        ]);
    }
}


To run this specific seeder, use the following command:

php artisan db:seed --class=CategoriesTableSeeder


This will insert the sample categories into the categories table.


Running a specific seeder in Laravel is a straightforward process. By using the --class option with the db:seed Artisan command, you can selectively populate your database tables with the data you need. This flexibility is valuable for efficiently managing and maintaining your database during development and testing.