where('uuid', $uuid)->withCount('hosts')->firstOrFail(); $this->uuid = $uuid; $this->name = $dc->name; $this->hostCount = $dc->hosts_count; } public function delete() { // Serialize the guard with the delete: lock the datacenter row and // re-check host membership inside the same transaction so a host created // concurrently can't be orphaned (hosts.datacenter is a bare code). DB::transaction(function () { $dc = Datacenter::query()->where('uuid', $this->uuid)->lockForUpdate()->first(); if ($dc === null) { return; } // Deactivate first so HostCreate's `exists:…,active,1` rule blocks any // new host for this code while we finish deleting. $dc->update(['active' => false]); if (Host::query()->where('datacenter', $dc->code)->exists()) { return; // still has hosts — leave it (deactivated) } $dc->delete(); }); return $this->redirectRoute('admin.datacenters', navigate: true); } public function render() { return view('livewire.admin.confirm-delete-datacenter'); } }