43
0

Laravel Target class [DatabaseSeeder] does not exist – solved

43
Reading Time: < 1 minute

I experienced this error Laravel Target class [DatabaseSeeder] does not exist, a few days ago after migrating a project from laravel 5 to laravel 8. You’ll notice it when running your database seeder

Step 1 – Check namespace on compose file

First step to solving Laravel Target class DatabaseSeeder not found is to check the compose file, the psr-4 key which defines namespaces to paths, relative to root package
Learn more on PSR-4


Specify the Database namespace if its missing or just correct the file to the one which references your seeds directory .i.e

…..

"autoload": {

"psr-4": {

    "App\\": "app/",

    "Database\\Factories\\": "database/factories/",

    "Database\\Seeders\\": "database/seeders/"

}

},

…..

Step 2 – Add namespace to files

Add the namespace to the seeders i.e

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{

Step 3 – Regenerate included classes – composer dump-autoload

Run composer dump-autoload this regenerates the list of all the classes that need to be included in your project

This solution was aimed at fixing Laravel Target class [DatabaseSeeder] does not exist or not found when you run your migrations in laravel. I hope it helps you.

 

Leave a Reply

Your email address will not be published.