*/ class AuthorFactory extends Factory { /** * The current User being used by the factory. */ protected static User $user; /** * Define the model's default state. * * @return array */ 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, ]); } }