phplaravellaravel-seeding

Target class [Database\Seeders\UsersTableSeeder] does not exist


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);
    }
}

Solution

  • 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.