From 50ad5f45ebf6b019d3dd75eb1ce4090fb47f0306 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 30 Jun 2026 16:44:13 +0800 Subject: [PATCH] fix: resolve permission/role cache not reflecting issues - UserController: use Spatie syncRoles([$role]) instead of direct Eloquent relations relation sync() which bypassed Spatie model events and cache observers - RoleController: explicitly call forgetCachedPermissions() on store and update to force cache refresh - Clear application cache --- app/Http/Controllers/RoleController.php | 2 ++ app/Http/Controllers/UserController.php | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php index 487160d35..7d82088e2 100644 --- a/app/Http/Controllers/RoleController.php +++ b/app/Http/Controllers/RoleController.php @@ -171,6 +171,7 @@ class RoleController extends BaseController if ($role) { $role->syncPermissions($validatedPermissions); + app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions(); return redirect()->route('roles.index')->with('success', __('Role created successfully with Permissions!')); } @@ -215,6 +216,7 @@ class RoleController extends BaseController // Update the permissions $role->syncPermissions($validatedPermissions); + app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions(); return redirect()->route('roles.index')->with('success', __('Role updated successfully with Permissions!')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 2b5832121..5cf0969e3 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -190,10 +190,13 @@ class UserController extends BaseController $role = Role::where('id', $request->roles) ->where('created_by', $created_by)->first(); - $user->roles()->sync([$role->id]); + $user->syncRoles([$role]); $user->type = $role->name; $user->save(); + // Clear Spatie permission cache + app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions(); + // Trigger email notification event(new \App\Events\UserCreated($user, $request->password)); @@ -230,11 +233,14 @@ class UserController extends BaseController $role = Role::where('id', $request->roles) ->where('created_by', $created_by)->first(); - $user->roles()->sync([$role->id]); + $user->syncRoles([$role]); $user->type = $role->name; } $user->save(); + + // Clear Spatie permission cache + app(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions(); return redirect()->route('users.index')->with('success', __('User updated with roles')); } return redirect()->back()->with('error', __('Unable to update User. Please try again!'));