How can I run all seeders within database/seeds folder automatically in laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Automating Seeders in Laravel: Moving Beyond Manual Execution

When developing large-scale Laravel applications, managing data initialization is a critical step. By default, Laravel relies on developers manually listing seeders within the DatabaseSeeder.php file to ensure that the correct sequence of data population occurs during testing or initial setup. The question arises: Is there a way to automate running all seeders located in the database/seeders directory without tedious manual entry?

The short answer is yes, it is technically possible. However, as any senior developer knows, automating processes requires careful consideration. We must weigh the convenience of automation against the necessity of control and dependency management.

The Manual vs. Automated Philosophy

As pointed out in the community discussions, while automating seeding seems efficient, we should consider that this approach can introduce significant problems if not handled correctly. Manual execution provides explicit control over the order of operations, which is essential when dealing with complex relational data dependencies.

When you manually list seeders in DatabaseSeeder.php, you are explicitly defining the sequence: Seed A must run before Seed B. Automation risks breaking this implicit dependency chain unless the automation mechanism is specifically designed to respect those relationships or enforces a strict, predefined order.

Method 1: The Standard Approach (Manual Control)

The canonical way Laravel handles seeding is through the DatabaseSeeder class. This file acts as an orchestrator, calling other seeders in sequence.

Example of Manual Setup:

// database/seeders/DatabaseSeeder.php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UserSeeder::class); // Explicit order defined here
        $this->call(ProductSeeder::class);
        $this->call(OrderSeeder::class);
    }
}

This method is safe, readable, and guarantees the execution flow. It aligns perfectly with best practices for maintaining application integrity, which is a core principle championed by frameworks like Laravel.

Method 2: The Automated Approach (Dynamic Loading)

If your goal is to run every seeder file in the directory without manually updating DatabaseSeeder.php, you need a dynamic approach that reads the filesystem and executes the corresponding classes. This typically involves using PHP's Reflection API or simple file iteration.

Here is a conceptual example demonstrating how you might achieve this via a custom Artisan command:

// app/Console/Commands/RunAllSeeders.php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Arr;

class RunAllSeeders extends Command
{
    protected $signature = 'seeder:run-all';
    protected $description = 'Runs all seeders found in the database/seeders directory.';

    public function handle()
    {
        $seeders = dir(database_path('seeders'));

        foreach ($seeders as $file) {
            if (is_file($file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
                // Dynamically load the class file
                require $file; 
                
                // Determine the class name (e.g., Database/Seeders/UserSeeder.php -> App\Models\UserSeeder)
                $className = "App\\Database\\Seeders\\" . basename($file, '.php');

                if (class_exists($className)) {
                    $this->info("Running seeder: {$className}");
                    // Execute the seed method defined in the class
                    Artisan::call($className . '@run'); 
                } else {
                    $this->error("Could not find class for file: {$file}");
                }
            }
        }

        $this->info('All seeders have been processed.');
    }
}

Note on Implementation: While the above logic demonstrates the concept, in a production environment, handling namespaces and autoloading correctly requires deeper integration with Laravel's Service Container. For seamless interaction with Laravel components, leveraging official tools or package structures is often safer than deep file system manipulation alone. Always consult documentation when building custom Artisan commands to ensure they integrate smoothly with the framework architecture, as detailed in resources from https://laravelcompany.com.

Conclusion: Prioritizing Control Over Convenience

Automating seeders offers a shortcut, but it sacrifices explicit control over the application state. For most standard applications, maintaining the manual sequence in DatabaseSeeder.php is the superior practice. It forces you to think about data flow and dependencies before execution.

Use automated scripts like the one above only for specific, non-dependent tasks or in highly controlled environments where the order of execution is strictly defined externally. Remember, when building robust systems, prioritize clarity and predictability over sheer automation.