*/ class BlogPostFactory extends Factory { protected $model = BlogPost::class; /** * Define the model's default state. * * @return array */ public function definition() { $imagePath = storage_path('app/public'); $curImage = $this->faker->image($imagePath.'/blog_posts', 360, 360, 'animals', true, true, 'cats', true, 'jpg'); $curImage = Str::replace($imagePath, '', $curImage); return [ 'state' => $this->faker->randomElement(['active', 'archived']), 'image' => $curImage, 'description' => $this->faker->realText($this->faker->numberBetween(100, 200)), 'youtube_url' => $this->faker->url(), 'created_at' => $this->faker->dateTimeBetween('-1 week', '+1 week'), 'updated_at' => $this->faker->dateTimeBetween('-1 week', '+1 week'), 'title' => $this->faker->realText($this->faker->numberBetween(10, 20)), ]; } public function some() { User::orderBy('created_at', 'desc')->limit(20)->get()->map(function ($user) { $newBlogPosts = BlogPost::factory()->count(3)->create(); $newBlogPosts->map(function ($post) use ($user) { $user->author_of_blog_post()->attach($post->id); }); }); } }