Files
eatvio-infra/database/factories/BlogPostFactory.php
2023-12-29 10:58:52 +03:00

48 lines
1.5 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\BlogPost;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BlogPost>
*/
class BlogPostFactory extends Factory
{
protected $model = BlogPost::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
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);
});
});
}
}