1 | <?php
|
---|
2 |
|
---|
3 | use Faker\Generator as Faker;
|
---|
4 |
|
---|
5 | $factory->define(App\Models\Post::class, function (Faker $faker) {
|
---|
6 |
|
---|
7 | $adminsAndEditors = App\Models\User::select("id")->where("role_id", 1)->orWhere("role_id", 2)->get();
|
---|
8 |
|
---|
9 | return [
|
---|
10 | "user_id" => $faker->numberBetween(1, 50),
|
---|
11 | "category_id" => $faker->randomElement($array = array(1, 2, 3, 4)),
|
---|
12 | "confirmed_by" => $faker->randomElement($array = $adminsAndEditors),
|
---|
13 | "title" => ucfirst($faker->words(4, true)),
|
---|
14 | "slug" => $faker->slug,
|
---|
15 | "image_link" => $faker->image(public_path() . '/uploads', 1024, 768, 'city', false),
|
---|
16 | "content" => $faker->realText(),
|
---|
17 | "is_active" => $faker->boolean,
|
---|
18 | "total_likes" => $faker->randomDigit,
|
---|
19 | "is_confirmed" => $faker->boolean,
|
---|
20 | ];
|
---|
21 | });
|
---|
22 |
|
---|
23 | $factory->afterCreating(\App\Models\Post::class, function ($post, $faker) {
|
---|
24 |
|
---|
25 | for ($i=0; $i<rand(20, 50); $i++) {
|
---|
26 |
|
---|
27 | if(!$post->is_confirmed || !$post->is_active) continue;
|
---|
28 |
|
---|
29 | $post->like()->create([
|
---|
30 | "post_id" => $post->id,
|
---|
31 | "ip_address" => $faker->ipv4
|
---|
32 | ]);
|
---|
33 |
|
---|
34 | $post->comment()->create([
|
---|
35 | "post_id" => $post->id,
|
---|
36 | "name" => $faker->name,
|
---|
37 | "email" => $faker->email,
|
---|
38 | "comment" => $faker->text,
|
---|
39 | "is_active" => $faker->boolean,
|
---|
40 | ]);
|
---|
41 |
|
---|
42 | $post->increment("total_likes");
|
---|
43 | $post->save();
|
---|
44 | }
|
---|
45 | });
|
---|