From f0c4df67a8c05fcfa73fa133e0320761ef612213 Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Wed, 13 Dec 2023 14:50:53 +0300 Subject: [PATCH] 'created seeder plan comments' --- database/factories/PlanCommentFactory.php | 28 ++++++++++ database/seeders/PlanCommentSeeder.php | 62 +++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100755 database/factories/PlanCommentFactory.php create mode 100755 database/seeders/PlanCommentSeeder.php diff --git a/database/factories/PlanCommentFactory.php b/database/factories/PlanCommentFactory.php new file mode 100755 index 0000000..eb18786 --- /dev/null +++ b/database/factories/PlanCommentFactory.php @@ -0,0 +1,28 @@ + + */ +class PlanCommentFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + + return [ + 'grade' => $this->faker->randomDigit(), + 'body' => $this->faker->text(), + ]; + + } +} diff --git a/database/seeders/PlanCommentSeeder.php b/database/seeders/PlanCommentSeeder.php new file mode 100755 index 0000000..949e3cc --- /dev/null +++ b/database/seeders/PlanCommentSeeder.php @@ -0,0 +1,62 @@ +id(); + // $table->bigInteger('user_id'); + // $table->bigInteger('plan_id'); + // $table->smallInteger('grade')->default(0); + // $table->longText('body')->nullable(); + // $table->timestamps(); + // + // return [ + // 'user_id' => User::count() > 0 ? function(User::) + // 'plan_id' + // 'body' => $this->faker->text(), + // ]; + // + // public function author() + // { + // return $this->belongsTo(User::class, 'user_id'); + // } + + // public function plan() + // { + // return $this->belongsTo(Plan::class, 'plan_id'); + // } + + if (User::count() > 0 && Plan::count() > 0) { + $comments = PlanComment::factory()->count(5)->make(); + + $comments->each(function ($comment) { + $comment->author()->associate( + User::inRandomOrder() + ->first() + ->id + ); + + $comment->plan()->associate( + Plan::inRandomOrder() + ->first() + ->id + ); + + $comment->saveOrFail(); + }); + + } + } +}