Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

php artisan db:seed cannot find your seeder class

Error[ReflectionException] Class SongsTableSeeder does not exist

Short answer

Artisan looks for your seeder through Composer's autoloader. Since Laravel 8 that means a Database\Seeders namespace and the database/seeders folder. On older projects the class must be in Composer's classmap, so composer dump-autoload is needed. Check the namespace line and the autoload block in composer.json first.

  • Applies to Laravel 12, Laravel 7, Composer 2, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which folder holds the seeder and which namespace and autoload rule are in play.

    ls database/
    head -n 8 database/seeders/SongsTableSeeder.php
    grep -n -A8 '"autoload"' composer.json
  2. 2Match what you see, then jump to its fix

    1. 1The file sits in database/seeders but starts with <?php and then straight into 'class'The seeder has no Database\Seeders namespace (Laravel 8 and newer)Fix
    2. 2The folder is database/seeds, and composer.json autoload still has a classmap entryA project upgraded from Laravel 7: database/seeds and the classmap block are still thereFix
    3. 3The file exists and the name is right, but it was created or copied after the last composer runA new seeder was added to a classmap project and the autoloader was not regenerated (Laravel 7 and older)Fix

What does the error look like?

  • [ReflectionException] Class SongsTableSeeder does not exist

Cause 1 of 3

The seeder has no Database\Seeders namespace (Laravel 8 and newer)

1Check

By default db:seed runs Database\Seeders\DatabaseSeeder, and seeders live in database/seeders. A class without the namespace line is not found at its expected name.

grep -n "namespace" database/seeders/*.php

2Fix

Add the namespace to every seeder, and make sure composer.json maps it. Then rebuild the autoloader.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class SongsTableSeeder extends Seeder
{
    public function run(): void { /* ... */ }
}

3Verify

Run that seeder on its own.

php artisan db:seed --class=SongsTableSeeder

Confirmed by laravel.com

Did this fix it?

Cause 2 of 3

A project upgraded from Laravel 7: database/seeds and the classmap block are still there

1Check

Laravel 8 moved seeders to database/seeders with a namespace. The upgrade guide says to rename the folder, add the namespace and replace the classmap block with PSR-4 mappings.

ls -d database/seeds database/seeders
grep -n -E "classmap|Database" composer.json

2Fix

Rename the folder, add the namespace to each class, and use these mappings in composer.json. Then dump the autoloader.

git mv database/seeds database/seeders

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
}

composer dump-autoload

3Verify

Seed again.

php artisan db:seed

Confirmed by laravel.com

Did this fix it?

Cause 3 of 3

A new seeder was added to a classmap project and the autoloader was not regenerated (Laravel 7 and older)

1Check

Classmap autoloading is built by scanning files, so a new class stays unknown until the map is rebuilt.

composer dump-autoload

2Fix

Regenerate the autoloader, then run the seeder. The Laravel 7 seeding page asks for this step after you write a seeder.

composer dump-autoload
php artisan db:seed --class=SongsTableSeeder

3Verify

The seeder runs without the ReflectionException.

php artisan db:seed --class=SongsTableSeeder

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Generate seeders with php artisan make:seeder so the namespace and folder are right.
  • After copying files in by hand, run composer dump-autoload.
  • Keep one seeder per file, with the file name equal to the class name.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require maatwebsite/excel fails to install

Composer says the package needs an older PHP than the one installed

Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.

3 causes, each with a fix

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.