editingId = $s->id; foreach (['key', 'eyebrow', 'title', 'body', 'button_label', 'button_url', 'is_visible', 'sort_order'] as $field) { $this->$field = $s->$field ?? (is_bool($this->$field) ? false : ''); } } public function add(): void { $this->reset(['editingId', 'key', 'eyebrow', 'title', 'body', 'button_label', 'button_url', 'sort_order']); $this->is_visible = true; } public function save(): void { $data = $this->validate([ 'key' => ['required', 'alpha_dash', 'max:80', Rule::unique('site_sections', 'key')->ignore($this->editingId)], 'eyebrow' => 'nullable|string|max:255', 'title' => 'required|string|max:255', 'body' => 'nullable|string|max:5000', 'button_label' => 'nullable|string|max:100', 'button_url' => 'nullable|string|max:500', 'is_visible' => 'boolean', 'sort_order' => 'integer|min:0|max:999', ]); SiteSection::updateOrCreate(['id' => $this->editingId], $data); $this->add(); session()->flash('status', 'Section saved successfully.'); } public function toggle(int $id): void { $s = SiteSection::findOrFail($id); $s->update(['is_visible' => ! $s->is_visible]); } public function delete(int $id): void { SiteSection::findOrFail($id)->delete(); if ($this->editingId === $id) { $this->add(); } } public function render() { return view('livewire.admin.sections', ['sections' => SiteSection::orderBy('sort_order')->get()])->layout('layouts.admin', ['title' => 'Page sections']); } }