diff --git a/app/Components/Role/Manager.php b/app/Components/Role/Manager.php
index 5d9fdfe..256d5ba 100644
--- a/app/Components/Role/Manager.php
+++ b/app/Components/Role/Manager.php
@@ -6,51 +6,30 @@ use Livewire\Component;
use Livewire\Attributes\On;
use Livewire\WithPagination;
use Spatie\Permission\Models\Role;
+use Spatie\Permission\Models\Permission;
class Manager extends Component
{
use WithPagination;
- /**
- * Các tùy chọn số bản ghi mỗi trang.
- *
- * @var int[]
- */
- public array $perPageOptions = [5, 10, 25, 50, 100];
-
- /**
- * Số bản ghi mỗi trang (mặc định).
- */
- public int $perPage = 10;
-
- /**
- * Giao diện phân trang (bootstrap hoặc tailwind).
- */
- protected string $paginationTheme = 'bootstrap';
+ public array $perPageOptions = [5, 10, 25, 50, 100];
+ public int $perPage = 10;
+ protected string $paginationTheme = 'bootstrap';
+ public string $mode = 'index';
+ public ?int $editingId = null;
+ public string $search = '';
+ public string $name = '';
- /**
- * Chế độ UI: 'index' (danh sách) hoặc 'form' (tạo/sửa).
- */
- public string $mode = 'index';
+ public array $allPermissions = [];
+ public array $assignedPermissions = [];
- /**
- * ID của role đang sửa (null khi tạo mới).
- */
- public ?int $editingId = null;
-
- /**
- * Chuỗi tìm kiếm hiện tại.
- */
- public string $search = '';
-
- /**
- * Tên role dùng trong form.
- */
- public string $name = '';
+ public function mount()
+ {
+ $this->allPermissions = Permission::orderBy('name')
+ ->get(['id', 'name'])
+ ->toArray();
+ }
- /**
- * Khi thay đổi số bản ghi mỗi trang, reset về trang 1.
- */
public function updatedPerPage(int $value): void
{
$this->resetPage();
@@ -59,10 +38,11 @@ class Manager extends Component
#[On('roleSaved')]
public function showIndex(): void
{
- $this->mode = 'index';
+ $this->mode = 'index';
$this->resetPage();
- $this->name = '';
- $this->editingId = null;
+ $this->name = '';
+ $this->editingId = null;
+ $this->assignedPermissions = [];
}
public function updatedSearch(string $value): void
@@ -74,11 +54,23 @@ class Manager extends Component
{
$this->mode = 'form';
$this->editingId = $id;
- $this->name = $id
- ? Role::findOrFail($id)->name
- : '';
+
+ if ($id) {
+ $role = Role::findOrFail($id);
+ $this->name = $role->name;
+ $this->assignedPermissions = $role
+ ->permissions
+ ->pluck('id')
+ ->toArray();
+ } else {
+ $this->name = '';
+ $this->assignedPermissions = [];
+ }
}
+ /**
+ * Delete a role by its ID.
+ */
public function delete(int $id): void
{
Role::findOrFail($id)->delete();
@@ -92,14 +84,18 @@ class Manager extends Component
. ($this->editingId ? ',' . $this->editingId : '');
$this->validate([
- 'name' => "required|string|{$uniqueRule}",
+ 'name' => "required|string|{$uniqueRule}",
+ 'assignedPermissions.*' => 'integer|exists:permissions,id',
]);
- Role::updateOrCreate(
+ $role = Role::updateOrCreate(
['id' => $this->editingId],
['name' => $this->name]
);
+ // Sync permissions theo ID
+ $role->permissions()->sync($this->assignedPermissions);
+
session()->flash('message', 'Role saved.');
$this->dispatch('roleSaved');
}
@@ -124,7 +120,7 @@ class Manager extends Component
public function render()
{
- $roles = Role::query()
+ $roles = Role::with('permissions') // ← Eager-load permissions
->when($this->search, fn($q) => $q->where('name', 'like', "%{$this->search}%"))
->orderByDesc('id')
->paginate($this->perPage);
diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php
index fa1899f..c4a44ec 100644
--- a/app/Http/Controllers/DashboardController.php
+++ b/app/Http/Controllers/DashboardController.php
@@ -7,7 +7,6 @@ class DashboardController extends Controller
{
public function index()
{
- // Trả về Blade wrapper, Livewire component sẽ được gọi trong đó
return view('admin.dashboard');
}
}
diff --git a/resources/views/admin/dashboard.blade.php b/resources/views/admin/dashboard.blade.php
index 5504cd1..eeac3fd 100644
--- a/resources/views/admin/dashboard.blade.php
+++ b/resources/views/admin/dashboard.blade.php
@@ -3,6 +3,5 @@
@section('title', 'Dashboard')
@section('content')
- {{-- Đây chính là chỗ Livewire inject component --}}