word-salad/database/factories/AuthorFactory.php

51 lines
1.3 KiB
PHP
Raw Permalink Normal View History

2024-08-04 04:49:42 +00:00
<?php
namespace Database\Factories;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Author>
*/
class AuthorFactory extends Factory
{
/**
* The current User being used by the factory.
*/
protected static User $user;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'display_name' => fake()->userName(),
'profile_image_url' => fake()->imageUrl(),
'user_id' => static::$user->id ??= User::factory()->create()->id,
'biography' => fake()->paragraph(),
'primary_link' => fake()->url(),
'secondary_link' => rand(0,1) == 0 ? fake()->url() : null,
'mastodon_handle' => rand(0,1) == 0 ? fake()->userName() : null,
];
}
/**
* Indicate the model's associated User
*/
public function user(int|User $user): static
{
if(is_int($user)) {
$user = User::findOrFail($user);
}
static::$user = $user;
return $this->state(fn (array $attributes) => [
'user_id' => $user->id,
]);
}
}