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?
3 Answers 3
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([
1 Comment
In the UserSeeder Class add:
use Illuminate\Support\Facades\DB;
1 Comment
I have fixed same error in Laravel 9 by importing
use Illuminate\Database\Seeder;
DB
class to use it in that file like thatuse DB;
after the namespace declarationcomposer dump-autoload
after creating a new seeder class or changing its namespace.