* * Note: UserObserver auto-creates a user_profiles row whenever a User is * created. To avoid unique-constraint collisions, this factory cooperates * with the observer: it updates the existing profile row (if any) rather * than blindly inserting a new one. */ final class UserProfileFactory extends Factory { /** @return array */ public function definition(): array { return [ 'user_id' => User::factory(), 'bio' => fake('nl_NL')->sentence(10), 'photo_url' => null, 'emergency_contact_name' => fake('nl_NL')->name(), 'emergency_contact_phone' => fake('nl_NL')->phoneNumber(), 'reliability_score' => fake()->randomFloat(2, 3.00, 5.00), 'is_ambassador' => false, 'settings' => null, ]; } public function ambassador(): static { return $this->state(fn () => ['is_ambassador' => true]); } public function newModel(array $attributes = []): UserProfile { $userId = $attributes['user_id'] ?? null; if ($userId !== null) { $existing = UserProfile::where('user_id', $userId)->first(); if ($existing !== null) { $existing->forceFill($attributes)->save(); return $existing; } } return parent::newModel($attributes); } }