问题
Illuminate\Contracts\Container\BindingResolutionException
Target class [Database\Seeders\CountriesTableSeeder] does not exist.
at C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:811
807▕
808▕ try {
809▕ $reflector = new ReflectionClass($concrete);
810▕ } catch (ReflectionException $e) {
➜ 811▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
812▕ }
813▕
814▕ // If the type is not instantiable, the developer is attempting to resolve
815▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionException::("Class Database\Seeders\CountriesTableSeeder does not exist")
2 C:\......\blog\vendor\laravel\framework\src\Illuminate\Container\Container.php:809
ReflectionClass::__construct("Database\Seeders\CountriesTableSeeder")
回答1:
From laravel 8 Seeders and factories are now namespaced
To accommodate for these changes, add Database\Seeders namespace to your seeder classes.
namespace Database\Seeders;
In addition, move all seeder files from previous database/seeds directory to database/seeders folder.
In your case remove all lines started with 'use Database\Seeders\ ...' from DatabaseSeeder.php file
It should solve the issue,
You can also run dump-autoload & fresh migration with seed,
composer dump-autoload
php artisan migrate:fresh --seed
回答2:
For Laravel 8 you need to make below changes for Seeding to work :-
Add Database\Seeders namespace at top of DatabaseSeeder.php and other Seeder files :-
namespace Database\Seeders;
Replace folder name seeds to seeders located at
\database\
folder.Update composer.json like below:
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
Finally, run below commands :-
composer dump-autoload php artisan db:seed
Hope it will help someone in future!
回答3:
After running: composer dump:autoload
I could just include the below code in my DatabaseSeeder
and It would include the data from SubsidyregimeTableSeeder
$this->call([
SubsidyregimeTableSeeder::class
]);
回答4:
DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
CountriesTableSeeder::class,
ProvincesTableSeeder::class,
RegionsTableSeeder::class,
PermisionsTableSeeder::class,
RolesTableSeeder::class,
StatusTypesTableSeeder::class,
StatusesTableSeeder::class,
]);
}
}
CountriesTableSeeder.php
<?php
use App\Country;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class CountriesTableSeeder extends Seeder
{
private $numberOfCountries = 10;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('countries')->insert([
['country_name' => 'iraq'],
['country_name' => 'qater'],
]);
}
}
来源:https://stackoverflow.com/questions/64116559/in-laravel-8-with-seeding-i-has-this-issue-target-class-tableseeder-does-not