mode = 'index'; $this->resetPage(); $this->name = ''; $this->editingId = null; } // Khi $search đã được cập nhật, reset pagination về trang 1 public function updatedSearch($value) { $this->resetPage(); } // Chuyển sang form (create/edit) public function showForm(int $id = null) { $this->mode = 'form'; $this->editingId = $id; $this->name = $id ? Permission::findOrFail($id)->name : ''; } // Xóa public function delete(int $id) { Permission::findOrFail($id)->delete(); session()->flash('message', 'Permission deleted.'); $this->resetPage(); } // Lưu form public function save() { $rules = [ 'name' => 'required|string|unique:permissions,name' . ($this->editingId ? ",{$this->editingId}" : '') ]; $this->validate($rules); Permission::updateOrCreate( ['id' => $this->editingId], ['name' => $this->name] ); session()->flash('message', 'Permission saved.'); // Gọi sự kiện nội bộ Livewire 3 $this->dispatch('permissionSaved'); } // Render view public function render() { $permissions = Permission::where('name', 'like', "%{$this->search}%") ->orderByDesc('id') ->paginate($this->perPage); return view('components.permission.manager', compact('permissions')); } }