LARAVEL 8: Class 'Database\Seeders\DB' not found
If you are seeing the error Class 'Database\Seeders\DB' not found while running a seeder, the culprit is almost certainly a missing import statement at the top of your file. In Laravel 8 and earlier versions, the global DB facade was automatically available within any class located in the database/seeders directory. However, this convenience feature has been deprecated and eventually removed in favor of stricter type safety and explicit dependency management.
When you execute php artisan db:seed --class=UserSeeder, Laravel instantiates your seeder class. If that class does not explicitly tell PHP where to find the DB facade, the autoloader cannot resolve it. Consequently, it throws a fatal error indicating it is looking for a class named Database\Seeders\DB. The error message specifically points to the line using DB::table('users'), confirming that the runtime environment expects a fully qualified class name but finds nothing in the current namespace.
To resolve this, you must manually import the Illuminate\Support\Facades\DB facade at the top of your UserSeeder.php file. This makes the code explicit and ensures compatibility across different Laravel versions and directory structures. Here is how the corrected UserSeeder should look:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB; // Explicitly imported
class UserSeeder extends Seeder
{
public function run()
{
foreach (range(1, 10) as $item) {
DB::table('users')->insert([
'name' => "name {$item}",
'email' => "email {$item}",
'email_verified_at' => now(),
'password' => "password {$item}"
]);
}
}
}
Once the import is added, running php artisan db:seed --class=UserSeeder will execute without errors. This change aligns your code with modern Laravel practices where facades should be imported when used within classes, rather than relying on implicit availability.
While adding the import is the direct fix, it is also worth considering whether you actually need to use a seeder for this task. If your goal is simply to populate the users table with dummy data for local development or testing, you might find that directly inserting records via Tinker is faster and less verbose. You can start Tinker with php artisan tinker and run the following commands:
DB::table('users')->insert([
'name' => "name 1",
'email' => "email 1",
'email_verified_at' => now(),
'password' => "password 1"
]);
This approach bypasses the seeder class entirely, avoiding any namespace or import issues altogether. However, seeders remain essential for automated testing suites or CI/CD pipelines where you need reproducible database states without manual intervention. In those scenarios, keeping the UserSeeder is the right choice, provided you keep it up to date with Laravel's evolving standards.
Another potential pitfall to avoid is hardcoding column names that might change in future migrations. For instance, if your users table has a password_hash column instead of password, or uses timestamps like created_at, ensure your seeder matches the actual schema defined in your migration files. Relying on implicit assumptions about database structure can lead to runtime errors even after fixing the class resolution issue.
If you are maintaining an older Laravel project and wish to avoid importing facades everywhere, you could consider switching to type-hinted dependency injection or using getFacadeApplication() methods, though these approaches add complexity that is rarely necessary for simple data seeding tasks. For most developers, adding a single use statement is the cleanest and most maintainable solution.