2
1

More than 1 year has passed since last update.

【Laravel】Class "Database\Factories\(モデル名)" not found

Last updated at Posted at 2022-06-27

2,3回同じエラーで5分ほど無駄にしてるのでいい加減忘れないために備忘録として。

環境

Laravel v9.5.1 (PHP v8.1.3)

状況

テストを実行した際に下記のエラー

Class "Database\Factories\Post" not found

解決法

protected $model = Post::class;を追記したときのuse App\Models\Post;の定義忘れが原因。
use App\Models\Post;を追記する。

namespace Database\Factories;

use App\Models\User;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
  protected $model = Post::class;

  public function definition(): array
  {
    return [
      'user_id' => User::factory(),
	  'title' => 'タイトル',
      'content' => '内容',
    ];
  }
}
2
1
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
2
1