4

I am getting the error "Target class [Database\Seeders\UsersTableSeeder] does not exist" and I cannot figure out why. I have already tried solutions posted to similar issues and none of them worked for me.

Here is my composer.json autoload/classmap settings

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "database/seeders",
        "database/factories"
    ]
},

UsersTableSeeder class

<?php

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

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'fname' => 'Billy',
            'lname'=> 'Bob',
            'email' => 'billy@gmail.com',
            'password' => Hash::make('12345678'),
        ]);
    }
}

DatabaseSeeder class


<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();
        $this->call(UsersTableSeeder::class);
    }
}
Community
  • 1
  • 1
Ken
  • 693
  • 1
  • 8
  • 14

2 Answers2

16

You are missing namespace in UsersTableSeeder class.

<?php

namespace Database\Seeders;
...

This will make autoloader to find the other seeder as they will be same namespace as DatabaseSeeder.

Note: run composer dump-autoload after that.

Dark Knight
  • 6,116
  • 1
  • 15
  • 37
  • 2
    I added the namespace and ran composer dump-autoload. It fixed the issue, thank you. – Ken Jan 15 '21 at 18:27
  • 1
    not resolving my issue. same problem persist – vishal-mote Mar 03 '22 at 13:03
  • 1
    So better, put more info, rather than down voting. You might wanna share your scenario, so we can also help you, this answer is specific to above described question. Also did you run `composer dump-autoload`? – Dark Knight Mar 03 '22 at 22:43
  • Running `composer dump-autoload` fixed my issue – Will Kim Aug 25 '22 at 08:41
  • database/seeds folder has been renamed to database/seeders in Laravel 8. If you upgrade from previous version, you might need to rename this folder. – Skyvory Aug 16 '23 at 08:53
0

Considering that the same error message appears if you incorrectly call a specific seeder, I would like to follow up on this topic even though the answer is not related to the above problem.

So if you run the command: php artisan db:seed --class=UsersTableSeeder, only this seeder will be executed. That's ok, but if you forget to add -- before the class, then you will get the same message.

I hope this helps someone

Darko
  • 11
  • 1
  • 4