authorize('plans.manage'); // modals bypass the route middleware $family = PlanFamily::query()->where('uuid', $uuid)->firstOrFail(); $this->uuid = $uuid; $this->audience = (string) $family->audience; $this->note = (string) $family->note; $this->recommended = (bool) $family->is_recommended; } public function save() { $this->authorize('plans.manage'); $data = $this->validate([ 'audience' => 'nullable|string|max:255', 'note' => 'nullable|string|max:500', 'recommended' => 'boolean', ]); $family = PlanFamily::query()->where('uuid', $this->uuid)->firstOrFail(); PlanFamily::query()->whereKey($family->getKey())->update([ 'audience' => $data['audience'] !== '' ? $data['audience'] : null, 'note' => $data['note'] !== '' ? $data['note'] : null, ]); // The singular invariant — only one family recommended at a time — // lives in the catalogue service, not here: two operators recommending // two different plans at the same moment must not both win. if ($data['recommended']) { app(PlanCatalogue::class)->recommend($family); } elseif ($family->is_recommended) { PlanFamily::query()->whereKey($family->getKey())->update(['is_recommended' => false]); } $this->dispatch('notify', message: __('plans.marketing_saved')); return $this->redirectRoute('admin.plans', navigate: true); } public function render() { return view('livewire.admin.edit-plan-family', [ // Read again rather than from the mounted properties: the header // shows the plan's name, and that can change under an operator's // own hands while a second tab has this modal open. 'family' => PlanFamily::query()->where('uuid', $this->uuid)->firstOrFail(), ]); } }