简体   繁体   中英

LARAVEL 8: Class 'Database\Seeders\DB' not found

I'm working with seeder to fill my users table, so I created a new seeder called UserSeeder and then I added these codes to it:

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

After that I tried php artisan db:seed --class=UserSeeder but it shows me:

Error

Class 'Database\Seeders\DB' not found

which is related to this line:

DB::table('users')->insert([

So why it is not found there, what should I do now?

That's because Laravel will look for DB class in the current namespace which is Database\\Seeders.

Since Laravel has facades defined in config/app.php which allows you to use those classes without full class name.

    'DB' => Illuminate\Support\Facades\DB::class,

You can either declare DB class after the namespace declaration with

use DB;

or just use it with backslash.

    \DB::table('users')->insert([

In the UserSeeder Class add:

use Illuminate\\Support\\Facades\\DB;

I have fixed same error in Laravel 9 by importing

`use Illuminate\Database\Seeder;`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM