word-salad/database/factories/ArticleFactory.php

53 lines
1.4 KiB
PHP
Raw Normal View History

2024-08-04 04:49:42 +00:00
<?php
namespace Database\Factories;
use App\Models\Author;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
*/
class ArticleFactory extends Factory
{
/**
* The current User being used by the factory.
*/
protected static Author $author;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => fake()->words(3, true),
'slug' => fake()->unique()->slug(),
'author_id' => static::$author->id ??= Author::factory()->create()->id,
'subtitle' => fake()->sentence(),
'body' => fake()->paragraphs(2, true),
'published_at' => fake()->date('Y-m-d H:m:s', now()->addDays(rand(0,20))),
'header_image_url' => fake()->imageUrl(),
'path_to_markdown_source' => fake()->name(),
'processed_at' => fake()->date('Y-m-d H:m:s'),
];
}
/**
* Indicate the model's associated Author
*/
public function author(int|Author $author): static
{
if(is_int($author)) {
$author = Author::findOrFail($author);
}
static::$author = $author;
return $this->state(fn (array $attributes) => [
'author_id' => $author->id,
]);
}
}