diff --git a/app/Livewire/Users.php b/app/Livewire/Users.php index 77ced0b..dc1bea3 100644 --- a/app/Livewire/Users.php +++ b/app/Livewire/Users.php @@ -4,6 +4,7 @@ namespace App\Livewire; use App\Models\Customer; use App\Models\Seat; +use Illuminate\Database\UniqueConstraintViolationException; use Illuminate\Support\Facades\DB; use Livewire\Attributes\Layout; use Livewire\Attributes\Validate; @@ -23,16 +24,19 @@ class Users extends Component public function mount(): void { - // Every customer starts with themselves as the owner seat. + // Every customer starts with themselves as the owner seat. firstOrCreate + // keyed on (customer_id, email) is idempotent; the catch covers the + // concurrent-first-visit race against the unique index. $customer = $this->customer(); if ($customer !== null && $customer->seats()->count() === 0) { - $customer->seats()->create([ - 'email' => $customer->email, - 'name' => $customer->name, - 'role' => 'owner', - 'status' => 'active', - 'invited_at' => now(), - ]); + try { + $customer->seats()->firstOrCreate( + ['email' => $customer->email], + ['name' => $customer->name, 'role' => 'owner', 'status' => 'active', 'invited_at' => now()], + ); + } catch (UniqueConstraintViolationException) { + // Another concurrent first visit created it — fine. + } } }