|
|
|
@ -1,13 +1,16 @@ |
|
|
|
|
<?php |
|
|
|
|
|
|
|
|
|
namespace App\Components\Permission; |
|
|
|
|
|
|
|
|
|
use Livewire\Component; |
|
|
|
|
use Livewire\Attributes\On; |
|
|
|
|
use Livewire\WithPagination; |
|
|
|
|
use Spatie\Permission\Models\Permission; |
|
|
|
|
|
|
|
|
|
class Manager extends Component |
|
|
|
|
{ |
|
|
|
|
use WithPagination; |
|
|
|
|
|
|
|
|
|
protected string $paginationTheme = 'bootstrap'; |
|
|
|
|
|
|
|
|
|
public string $mode = 'index'; // 'index' hoặc 'form' |
|
|
|
@ -15,9 +18,15 @@ class Manager extends Component |
|
|
|
|
public string $search = ''; // search query |
|
|
|
|
public string $name = ''; // dùng cho form |
|
|
|
|
|
|
|
|
|
protected $listeners = [ |
|
|
|
|
'permissionSaved' => 'showIndex', |
|
|
|
|
]; |
|
|
|
|
// Khi sự kiện 'permissionSaved' được dispatch, gọi showIndex() |
|
|
|
|
#[On('permissionSaved')] |
|
|
|
|
public function showIndex() |
|
|
|
|
{ |
|
|
|
|
$this->mode = 'index'; |
|
|
|
|
$this->resetPage(); |
|
|
|
|
$this->name = ''; |
|
|
|
|
$this->editingId = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Khi search thay đổi, reset pagination |
|
|
|
|
public function updatingSearch() |
|
|
|
@ -35,15 +44,6 @@ class Manager extends Component |
|
|
|
|
: ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Quay về index, refresh list |
|
|
|
|
public function showIndex() |
|
|
|
|
{ |
|
|
|
|
$this->mode = 'index'; |
|
|
|
|
$this->resetPage(); |
|
|
|
|
$this->name = ''; |
|
|
|
|
$this->editingId = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Xóa |
|
|
|
|
public function delete(int $id) |
|
|
|
|
{ |
|
|
|
@ -55,8 +55,10 @@ class Manager extends Component |
|
|
|
|
// Lưu form |
|
|
|
|
public function save() |
|
|
|
|
{ |
|
|
|
|
$rules = ['name' => 'required|string|unique:permissions,name' |
|
|
|
|
.($this->editingId ? ",{$this->editingId}" : '')]; |
|
|
|
|
$rules = [ |
|
|
|
|
'name' => 'required|string|unique:permissions,name' |
|
|
|
|
. ($this->editingId ? ",{$this->editingId}" : '') |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$this->validate($rules); |
|
|
|
|
|
|
|
|
@ -66,9 +68,12 @@ class Manager extends Component |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
session()->flash('message', 'Permission saved.'); |
|
|
|
|
$this->emit('permissionSaved'); |
|
|
|
|
|
|
|
|
|
// 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}%") |
|
|
|
|