resetPage(); } #[On('permissionSaved')] public function showIndex(): void { $this->mode = 'index'; $this->resetPage(); $this->name = ''; $this->editingId = null; } public function updatedSearch(string $value): void { $this->resetPage(); } public function showForm(?int $id = null): void { $this->mode = 'form'; $this->editingId = $id; $this->name = $id ? Permission::findOrFail($id)->name : ''; } public function delete(int $id): void { Permission::findOrFail($id)->delete(); session()->flash('message', 'Permission deleted.'); $this->resetPage(); } public function save(): void { $uniqueRule = 'unique:permissions,name' . ($this->editingId ? ',' . $this->editingId : ''); $this->validate([ 'name' => "required|string|{$uniqueRule}", ]); Permission::updateOrCreate( ['id' => $this->editingId], ['name' => $this->name] ); session()->flash('message', 'Permission saved.'); $this->dispatch('permissionSaved'); } protected function computeTitle(): string { if ($this->mode === 'index') { return 'Permissions'; } if ($this->mode === 'form' && $this->editingId) { return "Edit Permission: {$this->name}"; } return 'Create Permission'; } public function getTitleProperty(): string { return $this->computeTitle(); } public function render() { $permissions = Permission::query() ->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%")) ->orderByDesc('id') ->paginate($this->perPage); return view('components.permission.manager', [ 'permissions' => $permissions, 'title' => $this->title, ]); } }