Laravel & Backend8 min read

How to Implement Role-Based Access Control (RBAC) in Laravel

Md Shahed Alam
Md Shahed Alam
March 25, 2025
How to Implement Role-Based Access Control (RBAC) in Laravel

Role-based access control sounds complex but the concept is simple: users have roles, roles have permissions, and you check permissions before letting someone do something.

The manual approach

The simplest implementation adds a role column to your users table. Then you check it in middleware or policies:

// In a policy
public function update(User $user, Post $post): bool {
    return $user->role === 'admin' || $user->id === $post->user_id;
}

This works fine for simple apps with two or three roles. It breaks down when you have many roles with overlapping permissions.

A proper RBAC schema

For more flexibility, create three tables: roles, permissions, and pivot tables. A user has many roles. A role has many permissions.

// User model
public function roles(): BelongsToMany {
    return $this->belongsToMany(Role::class);
}

public function hasPermission(string $permission): bool {
    return $this->roles->flatMap->permissions->contains('name', $permission);
}

Using Spatie's package

The spatie/laravel-permission package is the industry standard. It handles all of this for you and integrates with Laravel's authorization system.

composer require spatie/laravel-permission
// Assign role
$user->assignRole('editor');

// Check permission
$user->can('edit articles');

// In middleware
Route::middleware(['role:admin'])->group(function () {
    // Admin-only routes
});

Policies vs middleware

Use policies for model-based authorization (can this user edit this specific post?). Use middleware for route-level access (can this user access admin routes at all?).

Together, they give you fine-grained control over every action in your application without cluttering your controllers.

LaravelBackend

Ready to build something great?

Let's talk about your project. We will give you honest advice, a clear plan, and a fair price. No pressure, no sales pitch.