Reading Time: 2 minutes

Laravel Target class DatabaseSeeder does not exist not found

In this quick post, we will solve the target class databaseseeder in Laravel 8 does not exist or not found. You may come across the target class does not exist as in the example below:


Illuminate\Contracts\Container\BindingResolutionException
Target class [Database\Seeders\UsersSeeder] does not exist.
at vendor/laravel/framework/src/Illuminate/Container/Container.php:879
875▕
876▕ try {
877▕ $reflector = new ReflectionClass($concrete);
878▕ } catch (ReflectionException $e) {
➜ 879▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);

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.

 

Categorized in: