0

macでlaravelアプリを動かしています。Laravelのseeder機能を使おうと思い、コードを書いたのですが、うまくいかず、下のようなエラーが起きてしまいます。よろしくお願いします。

イメージ説明

エラー文は以下の通りです

コード
$ php artisan db:seed

Deprecated: PHP Startup: Use of mbstring.internal_encoding is deprecated in Unknown on line 0

   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [PhotoTableSeeder] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:877
    873▕ 
    874▕         try {
    875▕             $reflector = new ReflectionClass($concrete);
    876▕         } catch (ReflectionException $e) {
  ➜ 877▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    878▕         }
    879▕ 
    880▕         // If the type is not instantiable, the developer is attempting to resolve
    881▕         // an abstract type such as an Interface or Abstract Class and there is

      +8 vendor frames 
  9   database/seeds/DatabaseSeeder.php:14
      Illuminate\Database\Seeder::call("PhotoTableSeeder")

      +23 vendor frames 
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

エラー文はなぜかわからないのですが、PhotoTableSeederを読み込めない、存在しないことになっているからだと考えております。
試したこととして、キャッシュを消したり、解決法の一つであるを実行したりしましたがうまくいきませんでした。

コード
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
composer dump-autoload

seeder機能を使う手順として、まずseederをartisanで作成し、このように記述しました。

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;


// 追加
use Illuminate\Support\Str;
use App\User;


// 追加(20220623)
use Illuminate\Support\Facades\DB;

class PhotoTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // 追加(20220623)
        $param = [
            'user_id' => fn () => \App\User::factory()->create()->id,
            'filename' => Str::random(12) . '.jpg',
            'created_at' => now(),
            'updated_at' => now(),
        ];
        DB::table('photos')->insert($param);
    }
}

次にseederファイルを登録するためにこのように記述しました。

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(PhotoTableSeeder::class);
    }
}

1 件の回答 1

1

https://teratail.com/questions/cvadmps35e88ui

terateilの方で解決いたしました。結論としてデフォルトで記述されていた、namespaceの部分が消えてしまっていたことによるエラーでした。ありがとうございました!

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。