LoginSignup
8
4

More than 3 years have passed since last update.

【Laravel】php artisan db:seedが通らない時の対処

Posted at

環境

PHP 7.3.8
Laravel 6.20.16
Docker 19.03.8

Target class [...] does not exist.というエラー

Docker環境でlaravelクイズアプリを作成中、
サンプルで問題とカテゴリーデータをシーダーでデータベースに入れようとした時に怒られた話。

php artisan db:seed

上記コマンドを実行した際に下記エラーが発生。

   Illuminate\Contracts\Container\BindingResolutionException  : Target class [CategoryTableSeeder] does not exist.

  at /var/www/html/app/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 CategoryTableSeeder does not exist")
      /var/www/html/app/vendor/laravel/framework/src/Illuminate/Container/Container.php:803

  2   ReflectionClass::__construct("CategoryTableSeeder")
      /var/www/html/app/vendor/laravel/framework/src/Illuminate/Container/Container.php:803

  Please use the argument -v to see more details.

結論から申し上げると、次のコマンドで解決した。

composer dump-autoload

ググると早々にそれっぽい記事は沢山出ていくるわけなんですが、
Docker環境で検証している時にdump-autoloadしてもダメじゃねーかと言う事があったんですよ。
でもそれってdump-autoloadを実行してからGenerating optimized autoload filesと表示された後に待ちきれずにcommand+Cしてて、ちゃんと実行出来てなかっただけと言うオチ。

Dockerだからか、結構実行終わるまで時間がかかるみたいで、最後まで待たないといけなかったと言う悲しい話。

root@:/var/www/html/app# composer dump-autoload
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
Generated optimized autoload files containing 4259 classes

最後まで見届けてから、db:seedする事で無事にseederを実行する事が出来ました。

root@:/var/www/html/app# php artisan db:seed
Seeding: CategoryTableSeeder
Seeded:  CategoryTableSeeder (0 seconds)
Seeding: QuizTableSeeder
Seeded:  QuizTableSeeder (0 seconds)
Database seeding completed successfully.
8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4