50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Enums\ArticleStatus;
|
|
use App\Models\Article;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ArticleFactory extends Factory
|
|
{
|
|
protected $model = Article::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
$title = $this->faker->sentence(6);
|
|
$status = $this->faker->randomElement(ArticleStatus::cases());
|
|
|
|
return [
|
|
'author_id' => User::factory(),
|
|
'title' => $title,
|
|
'slug' => Str::slug($title.'-'.$this->faker->unique()->randomNumber()),
|
|
'excerpt' => $this->faker->paragraph(),
|
|
'content' => $this->faker->paragraphs(3, true),
|
|
'status' => $status,
|
|
'published_at' => $status === ArticleStatus::PUBLISHED
|
|
? $this->faker->dateTimeBetween('-10 days', 'now')
|
|
: null,
|
|
'views' => $this->faker->numberBetween(0, 1000),
|
|
];
|
|
}
|
|
|
|
public function draft(): Factory
|
|
{
|
|
return $this->state(fn () => [
|
|
'status' => ArticleStatus::DRAFT,
|
|
'published_at' => null,
|
|
]);
|
|
}
|
|
|
|
public function published(): Factory
|
|
{
|
|
return $this->state(fn () => [
|
|
'status' => ArticleStatus::PUBLISHED,
|
|
'published_at' => now(),
|
|
]);
|
|
}
|
|
}
|