PHP Laravelを勉強しています。
ゼロから始めるPHP ~Laravelを使って掲示板を作る~を参考通りに作成しています。
php artisan db:seed
でseedを登録しようとしたのですが、Unable to locate factory for [App\Post].
([App\Post] のファクトリーが見つかりません。)とエラーが表示されました。
PostsTableSeederがPost.phpをうまく特定できていないのでしょうか?
・ディレクトリ構成
myapp ←実行時のディレクトリ
-app
-Console
-Exceptions
-Http
-Providers
Comment.php
Post.php
User.php
・PostFactory.php
<?php
use Faker\Generator as Faker;
$factory->define(Post::class, function (Faker $faker) {
return [
'title' => '投稿のタイトル',
'body' => "本文です。テキスト。",
];
});
・PostsTableSeeder
<?php
use Illuminate\Database\Seeder;
use App\Post;
use App\Comment;
class PostsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(Post::class, 50)
->create()
->each(function ($post) {
$comments = factory(App\Comment::class, 2)->make();
$post->comments()->saveMany($comments);
});
}
}
・エラー内容
ec2-user:~/environment/myapp $ php artisan db:seed
Seeding: PostsTableSeeder
InvalidArgumentException
Unable to locate factory for [App\Post].
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:273
269| */
270| protected function getRawAttributes(array $attributes = [])
271| {
272| if (! isset($this->definitions[$this->class])) {
> 273| throw new InvalidArgumentException("Unable to locate factory for [{$this->class}].");
274| }
275|
276| $definition = call_user_func(
277| $this->definitions[$this->class],
+4 vendor frames
5 [internal]:0
Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent\{closure}()
+2 vendor frames
8 database/seeds/PostsTableSeeder.php:17
Illuminate\Database\Eloquent\FactoryBuilder::create()
Mysqlバージョン:mysql Ver 8.0.33 for Linux on x86_64 (MySQL Community Server - GPL)
Laravelバージョン:Laravel Framework 7.30.6