[Laravel] Seeder実行時のエラー Target class [DatabaseSeeder] does not exist.
解決したいこと
Seederを使って国のデータを入れたい。
該当するソースコード
nationalities_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateNationalitiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('nationalities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('nationalities');
}
}
NationalitySeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class NationalitySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$params = [
['id' => 1, 'name' => 'アイスランド'],
['id' => 2, 'name' => 'アイルランド'],
['id' => 3, 'name' => 'アフガニスタン'],
['id' => 4, 'name' => 'アメリカ合衆国'],
['id' => 5, 'name' => 'アラブ首長国連邦'],
['id' => 6, 'name' => 'アルゼンチン'],
['id' => 7, 'name' => 'イギリス'],
['id' => 8, 'name' => 'イスラエル'],
['id' => 9, 'name' => 'イタリア'],
['id' => 10, 'name' => 'イラク'],
['id' => 11, 'name' => 'イラン'],
['id' => 12, 'name' => 'インド'],
['id' => 13, 'name' => 'インドネシア'],
['id' => 14, 'name' => 'ウクライナ'],
['id' => 15, 'name' => 'ウズベキスタン'],
['id' => 16, 'name' => 'ウルグアイ'],
['id' => 17, 'name' => 'エクアドル'],
['id' => 18, 'name' => 'エジプト'],
['id' => 19, 'name' => 'オーストラリア'],
['id' => 20, 'name' => 'オーストリア'],
['id' => 21, 'name' => 'オマーン'],
['id' => 22, 'name' => 'オランダ'],
['id' => 23, 'name' => 'ガーナ'],
['id' => 24, 'name' => 'カタール'],
['id' => 25, 'name' => 'カナダ'],
['id' => 26, 'name' => 'カメルーン'],
['id' => 27, 'name' => 'カンボジア'],
['id' => 28, 'name' => 'キューバ'],
['id' => 29, 'name' => 'ギリシャ'],
['id' => 30, 'name' => 'グアム'],
['id' => 31, 'name' => 'クロアチア'],
['id' => 32, 'name' => 'ケニア'],
['id' => 33, 'name' => 'コスタリカ'],
['id' => 34, 'name' => 'コロンビア'],
['id' => 35, 'name' => 'サウジアラビア'],
['id' => 36, 'name' => 'ジャマイカ'],
['id' => 37, 'name' => 'シンガポール'],
['id' => 38, 'name' => 'スイス'],
['id' => 39, 'name' => 'スウェーデン'],
['id' => 40, 'name' => 'スペイン'],
['id' => 41, 'name' => 'スリランカ'],
['id' => 42, 'name' => 'スロバキア'],
['id' => 43, 'name' => 'スロベニア'],
['id' => 44, 'name' => 'セネガル'],
['id' => 45, 'name' => 'セルビア'],
['id' => 46, 'name' => 'タイ'],
['id' => 47, 'name' => '大韓民国'],
['id' => 48, 'name' => '中国台湾省(中華民国)'],
['id' => 49, 'name' => 'タジキスタン'],
['id' => 50, 'name' => 'チェコ'],
['id' => 51, 'name' => '中華人民共和国'],
['id' => 52, 'name' => 'チュニジア'],
['id' => 53, 'name' => '朝鮮民主主義人民共和国'],
['id' => 54, 'name' => 'チリ'],
['id' => 55, 'name' => 'デンマーク'],
['id' => 56, 'name' => 'ドイツ'],
['id' => 57, 'name' => 'ドミニカ共和国'],
['id' => 58, 'name' => 'ドミニカ国'],
['id' => 59, 'name' => 'トルコ'],
['id' => 60, 'name' => 'ナイジェリア'],
['id' => 61, 'name' => '日本'],
['id' => 62, 'name' => 'ニュージーランド'],
['id' => 63, 'name' => 'ネパール'],
['id' => 64, 'name' => 'ノルウェー'],
['id' => 65, 'name' => 'バーレーン'],
['id' => 66, 'name' => 'バチカン市国'],
['id' => 67, 'name' => 'パナマ'],
['id' => 68, 'name' => 'パラオ'],
['id' => 69, 'name' => 'パラグアイ'],
['id' => 70, 'name' => 'ハンガリー'],
['id' => 71, 'name' => 'バングラデシュ'],
['id' => 72, 'name' => 'フィジー'],
['id' => 73, 'name' => 'フィリピン'],
['id' => 74, 'name' => 'フィンランド'],
['id' => 75, 'name' => 'フェロー諸島'],
['id' => 76, 'name' => 'ブラジル'],
['id' => 77, 'name' => 'フランス'],
['id' => 78, 'name' => 'ブルガリア'],
['id' => 79, 'name' => 'ベトナム'],
['id' => 80, 'name' => 'ベネズエラ・ボリバル共和国'],
['id' => 81, 'name' => 'ペルー'],
['id' => 82, 'name' => 'ベルギー'],
['id' => 83, 'name' => 'ポーランド'],
['id' => 84, 'name' => 'ボスニア・ヘルツェゴビナ'],
['id' => 85, 'name' => 'ポルトガル'],
['id' => 86, 'name' => '香港'],
['id' => 87, 'name' => 'ホンジュラス'],
['id' => 88, 'name' => 'マカオ'],
['id' => 89, 'name' => 'マダガスカル'],
['id' => 90, 'name' => 'マレーシア'],
['id' => 91, 'name' => '南アフリカ'],
['id' => 92, 'name' => 'ミャンマー'],
['id' => 93, 'name' => 'メキシコ'],
['id' => 94, 'name' => 'モナコ'],
['id' => 95, 'name' => 'モロッコ'],
['id' => 96, 'name' => 'モンゴル'],
['id' => 97, 'name' => 'モンテネグロ'],
['id' => 98, 'name' => 'ルーマニア'],
['id' => 99, 'name' => 'ルクセンブルク'],
['id' => 100, 'name' => 'ロシア連邦'],
];
DB::table('nationalities')->insert($params);
}
}
DatabeseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Database\Seeders\NationalitySeeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
// private const SEEDERS = [
// NationalitySeeder::class,
// ];
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// foreach(self::SEEDERS as $seeder) {
// $this->call($seeder);
// }
$this->call(NationalitySeeder::class);
}
}
エラー内容
.terminal
Illuminate\Contracts\Container\BindingResolutionException : Target class [DatabaseSeeder] does not exist.
at /home/ec2-user/environment/laravel_recipe/vendor/laravel/framework/src/Illuminate/Container/Container.php:805
801|
802| try {
803| $reflector = new ReflectionClass($concrete);
804| } catch (ReflectionException $e) {
> 805| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
806| }
807|
808| // If the type is not instantiable, the developer is attempting to resolve
809| // an abstract type such as an Interface or Abstract Class and there is
Exception trace:
1 ReflectionException::("Class DatabaseSeeder does not exist")
/home/ec2-user/environment/laravel_recipe/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
2 ReflectionClass::__construct("DatabaseSeeder")
/home/ec2-user/environment/laravel_recipe/vendor/laravel/framework/src/Illuminate/Container/Container.php:803
Please use the argument -v to see more details.
自分で試したこと
composer dump-autoloadを試してもダメでした。
1