Learn how to check if users have specific permissions and roles
Laravel Permission provides multiple methods to check if a user has specific permissions or roles. These checks work seamlessly with both direct permissions and permissions inherited through roles.
$user = User::find(1);// Check by permission nameif ($user->hasPermissionTo('edit articles')) { // User has the permission}// Check with specific guardif ($user->hasPermissionTo('edit articles', 'api')) { // User has the permission on api guard}// Using Permission model$permission = Permission::findByName('edit articles');if ($user->hasPermissionTo($permission)) { // User has the permission}
Throws PermissionDoesNotExist exception if the permission doesn’t exist in the database.
Safe version that doesn’t throw exceptions:
// Returns false if permission doesn't existif ($user->checkPermissionTo('edit articles')) { // User has the permission}// This won't throw an exception even if 'nonexistent' doesn't existif ($user->checkPermissionTo('nonexistent permission')) { // Will be false}
Use checkPermissionTo() when you’re not sure if the permission exists.
Laravel’s built-in authorization method also works:
// Laravel automatically registers permissions with the Gateif ($user->can('edit articles')) { // User can edit articles}// In controllers$this->authorize('edit articles');
The package automatically registers all permissions with Laravel’s Gate system.
Check if a user has any or all of multiple permissions.
hasAnyPermission()
hasAllPermissions()
canAny() - Laravel Native
Check if user has at least one of the given permissions:
// Returns true if user has ANY of these permissionsif ($user->hasAnyPermission(['edit articles', 'delete articles'])) { // User can either edit OR delete articles}// Works with variable argumentsif ($user->hasAnyPermission('edit articles', 'delete articles')) { // Same as above}
Check if user has all of the given permissions:
// Returns true only if user has ALL these permissionsif ($user->hasAllPermissions(['edit articles', 'publish articles'])) { // User can both edit AND publish articles}// Works with variable argumentsif ($user->hasAllPermissions('edit articles', 'publish articles')) { // Same as above}
Laravel’s native method for checking multiple permissions:
// Check if user can perform any of these actionsif ($user->canAny(['edit articles', 'delete articles'])) { // User can edit OR delete}
Check only directly assigned permissions, ignoring role permissions.
// Check if user has direct permission (not via role)if ($user->hasDirectPermission('edit articles')) { // User was directly given this permission}// Check multiple direct permissions (any)if ($user->hasAnyDirectPermission('edit articles', 'delete articles')) { // User has at least one direct permission}// Check multiple direct permissions (all)if ($user->hasAllDirectPermissions('edit articles', 'publish articles')) { // User has all these direct permissions}
These methods check ONLY direct permissions and ignore permissions inherited through roles.
// Check single role (string)if ($user->hasRole('writer')) { // User has the writer role}// Check with Role model$role = Role::findByName('writer');if ($user->hasRole($role)) { // User has the role}// Check multiple roles (returns true if user has ANY)if ($user->hasRole(['writer', 'editor'])) { // User is either a writer OR editor}// Using pipe separatorif ($user->hasRole('writer|editor')) { // Same as array}
Alias to hasRole() for checking multiple roles:
// More explicit method nameif ($user->hasAnyRole('writer', 'editor', 'admin')) { // User has at least one of these roles}if ($user->hasAnyRole(['writer', 'editor', 'admin'])) { // Same as above}
Check if user has all specified roles:
// Returns true only if user has ALL rolesif ($user->hasAllRoles(['writer', 'reviewer'])) { // User is both a writer AND reviewer}// With specific guardif ($user->hasAllRoles(['writer', 'reviewer'], 'api')) { // Check on specific guard}
Check if user has exactly these roles (no more, no less):
// Returns true only if user has EXACTLY these rolesif ($user->hasExactRoles(['writer', 'reviewer'])) { // User has exactly these two roles, no others}// If user has ['writer', 'reviewer', 'editor'], this returns false// If user has only ['writer'], this also returns false
// Check if has permission (throws exception if not found)public function hasPermissionTo( string|int|Permission|BackedEnum $permission, ?string $guardName = null): bool// Safe check (returns false if not found)public function checkPermissionTo( string|int|Permission|BackedEnum $permission, ?string $guardName = null): bool// Check any permissionpublic function hasAnyPermission( string|int|array|Permission|Collection|BackedEnum ...$permissions): bool// Check all permissionspublic function hasAllPermissions( string|int|array|Permission|Collection|BackedEnum ...$permissions): bool// Check direct permissionpublic function hasDirectPermission( string|int|Permission|BackedEnum $permission): bool
Common patterns for checking permissions in controllers:
Manual Checks
Using authorize()
Constructor Middleware
namespace App\Http\Controllers;class ArticleController extends Controller{ public function edit(Article $article) { if (! auth()->user()->can('edit articles')) { abort(403); } return view('articles.edit', compact('article')); }}
public function edit(Article $article){ // Throws 403 if user doesn't have permission $this->authorize('edit articles'); return view('articles.edit', compact('article'));}
class ArticleController extends Controller{ public function __construct() { // Check permission for all methods $this->middleware('can:edit articles'); // Check for specific methods $this->middleware('can:delete articles')->only('destroy'); }}
If wildcard permissions are enabled, you can use pattern matching:
// User has permission: 'articles.*'$user->givePermissionTo('articles.*');// These all return true:$user->hasPermissionTo('articles.edit');$user->hasPermissionTo('articles.delete');$user->hasPermissionTo('articles.publish');
Wildcard permissions must be enabled in the config:
Check permissions for users other than the authenticated user:
$user = User::find(5);// Check this specific user's permissionsif ($user->hasPermissionTo('edit articles')) { // This user can edit}// Compare with authenticated userif (auth()->user()->hasPermissionTo('delete articles') && !$user->hasPermissionTo('delete articles')) { // I can delete but the other user cannot}