Target class [Database\Seeders\UsersTableSeeder] does not exist
When you run php artisan db:seed and receive a Target class [Database\Seeders\UsersTableSeeder] does not exist error, the issue almost always stems from a mismatch between your file path, its namespace declaration, and Laravel's autoloading configuration. It is rarely a PHP syntax error or a missing database table; it is strictly an autoloader failure.
In the scenario provided, the DatabaseSeeder class resides in the Database\Seeders namespace and attempts to call UsersTableSeeder::class. However, for this call to resolve correctly, Laravel must be able to map that fully qualified class name to a physical file on disk. If the autoloader cannot find the file based on the namespace hints, it throws the "does not exist" exception.
The Namespace and File Path Mismatch
The most common culprit in this specific error is the location of the UsersTableSeeder file relative to its declared namespace.
Laravel follows PSR-4 autoloading standards. If you declare a class with the namespace Database\Seeders, Laravel expects that file to live inside the app/Database/Seeders directory structure. However, looking at your composer.json, you have explicitly configured a classmap entry for "database/seeders".
This configuration tells Composer to scan the root database/seeders directory and add those files to its class map, regardless of their namespace declarations. This approach works if your file structure is flat (no subdirectories), but it creates ambiguity when you mix PSR-4 conventions with classmap scanning.
If your UsersTableSeeder.php file is actually located at app/Database/Seeders/UsersTableSeeder.php, the classmap entry in composer.json is irrelevant for this specific file because Composer will already know where to find it via the psr-4 mapping of the App\ namespace (assuming your file uses namespace App\Database\Seeders;).
Conversely, if your file is located at database/seeders/UsersTableSeeder.php, but you have declared the namespace as Database\Seeders, the autoloader will look for it in the path defined by the base directory. If your base directory for that namespace is not correctly set up or if you are relying solely on the classmap entry without ensuring the file is physically present in that scanned location, the lookup fails.
Correcting the Configuration
To resolve this immediately, ensure your file paths align with your namespace declarations. The most robust solution in modern Laravel applications (8.x and 10.x) is to rely entirely on PSR-4 autoloading and remove manual classmap entries for seeders unless you have a specific legacy reason to keep them.
Option 1: Standard PSR-4 Structure
Move your seeder files into the standard Laravel directory structure under app:
app/Database/Seeders/UsersTableSeeder.php
Ensure the file content matches this namespace:
<?php
namespace App\Database\Seeders; // Note: Must match the path relative to app/
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class UsersTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->insert([
'fname' => 'Billy',
'lname' => 'Bob',
'email' => 'billy@gmail.com',
'password' => Hash::make('12345678'),
]);
}
}
Update your composer.json to remove the unnecessary classmap entries for these files, as they are now handled automatically by the psr-4 block:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
After making these changes, you must run Composer to rebuild the autoload files. This step is critical; without it, Laravel will continue using the cached class map that excludes your new file location:
composer dump-autoload
Then, test the seed command again:
php artisan db:seed --class=DatabaseSeeder
Option 2: Keeping the Root Directory Structure
If you prefer to keep your seeder files in a root-level database directory (outside of app), you must ensure the namespace declaration in the file matches the path implied by your autoloader configuration.
If you keep the file at database/seeders/UsersTableSeeder.php, your namespace should be Database\Seeders. However, your composer.json currently maps App\ to app/. It does not explicitly map Database\ to database/. By adding a classmap entry for "database/seeders", you are telling Composer: "Ignore namespaces here, just look at these files."
For this to work, the file must physically exist in that exact location. If you moved the file but forgot to update the path in composer.json, or if you declared a namespace like App\Database\Seeders while keeping the file in the root database folder, the autoloader will fail to find it because no PSR-4 rule points there, and the classmap might not be scanning the correct subdirectory depth.
Modernizing the Seeder Approach
While fixing the path issue resolves the immediate error, it is worth noting that manual seeders like UsersTableSeeder are often unnecessary in modern Laravel applications. The framework encourages using Factories for data seeding.
Instead of creating a separate class, you can define your seeder logic directly within the DatabaseSeeder or use the factory method:
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
// This is cleaner and leverages Laravel's built-in factories
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
]);
}
}
If you require custom raw SQL or specific hash logic as in your original example, the dedicated seeder class is valid, but it must adhere strictly to PSR-4 conventions. The key takeaway is consistency: ensure your namespace declaration matches the directory structure relative to the base autoload path, and always run composer dump-autoload after changing these configurations.
By aligning your file paths with your namespace declarations and clearing the autoloader cache, the Target class does not exist error will vanish, allowing your seed process to execute as intended.