Learn how to assign permissions and roles to users
Once you’ve created permissions and roles, you can assign them to your users. Laravel Permission provides flexible methods for managing user permissions both directly and through roles.
Your User model must use the HasRoles trait to access these methods.
Users can have multiple roles, and roles determine what permissions users have.
Assign Role
Remove Role
Sync Roles
Give one or more roles to a user:
use Spatie\Permission\Models\Role;$user = User::find(1);// Assign single role (string)$user->assignRole('writer');// Assign single role (Role model)$role = Role::findByName('writer');$user->assignRole($role);// Assign multiple roles$user->assignRole('writer', 'editor');$user->assignRole(['writer', 'editor']);
If the user already has the role, it won’t be assigned again. The method is idempotent.
Revoke roles from a user:
// Remove single role$user->removeRole('writer');// Remove multiple roles$user->removeRole('writer', 'editor');$user->removeRole(['writer', 'editor']);
Replace all roles with a new set:
// Remove all existing roles and assign only these$user->syncRoles(['writer', 'editor']);// Remove all roles$user->syncRoles([]);
syncRoles() removes ALL existing roles before assigning new ones.
Direct permissions are useful for granting special access to specific users without creating a new role.
Remove direct permissions from a user:
// Revoke single permission$user->revokePermissionTo('edit articles');// This only removes DIRECT permissions// If the user has the permission via a role, they'll still have access
Replace all direct permissions:
// Remove all direct permissions and assign only these$user->syncPermissions(['edit articles', 'publish articles']);// Remove all direct permissions$user->syncPermissions([]);
This only affects direct permissions, not permissions inherited through roles.
You can use both roles and direct permissions together:
$user = User::find(1);// Assign a base role$user->assignRole('writer');// Now user has: 'edit articles' (via role)// Grant additional direct permission$user->givePermissionTo('publish articles');// Now user has: 'edit articles' (via role) + 'publish articles' (direct)
Best Practice: Use roles for standard permissions and direct permissions for exceptions.Example: All writers can edit articles (via role), but only some writers can publish (via direct permission).
The user must be saved to the database before assigning roles or permissions. If you try to assign to an unsaved model, the assignments will be queued and applied when the model is saved.
// User with web guard$webUser = User::find(1);$webUser->assignRole('admin'); // Assigns web guard role// API user with api guard$apiUser = ApiUser::find(1);$apiUser->assignRole('admin'); // Assigns api guard role// Explicitly specify guard$user->assignRole(Role::findByName('admin', 'api'));
Roles and permissions must match the user’s guard. You’ll get a GuardDoesNotMatch exception if they don’t match.
// Get multiple users$users = User::whereIn('id', [1, 2, 3])->get();// Assign same role to allforeach ($users as $user) { $user->assignRole('writer');}// Or using query builderUser::whereIn('id', [1, 2, 3]) ->each(fn($user) => $user->assignRole('writer'));
Laravel Permission fires events when roles and permissions are assigned:
use Spatie\Permission\Events\RoleAttachedEvent;use Spatie\Permission\Events\PermissionAttachedEvent;// Listen for role assignmentsEvent::listen(RoleAttachedEvent::class, function ($event) { $model = $event->model; // The user $roleIds = $event->roleIds; // Array of role IDs});// Listen for permission assignmentsEvent::listen(PermissionAttachedEvent::class, function ($event) { $model = $event->model; // The user $permissionIds = $event->permissionIds; // Array of permission IDs});
Events are only fired if permission.events_enabled is set to true in your config.
// In User modelprotected static function booted(){ static::created(function ($user) { $user->assignRole('user'); });}
Promotion/Demotion
Change user roles based on actions:
public function promoteToEditor(User $user){ $user->removeRole('writer'); $user->assignRole('editor');}public function demoteToWriter(User $user){ $user->removeRole('editor'); $user->assignRole('writer');}
Temporary Permissions
Grant temporary additional permissions:
// Grant extra permission for specific task$user->givePermissionTo('delete articles');// Perform task$article->delete();// Revoke the permission$user->revokePermissionTo('delete articles');