Blog Detail

19

Sep
Simplify Role & Permission Management in Laravel with Ladder cover image

arrow_back Simplify Role & Permission Management in Laravel with Ladder

Ladder simplifies role and permission management for your Laravel project by avoiding storing everything in the database. Inspired by Laravel Jetstream, it offers a static approach, reducing queries and ensuring immutability for easy modifications.

Installation

This package requires Laravel 10 and above.

composer require eneadm/ladder

Once Ladder is installed, create a new LadderServiceProvider to manage roles and permissions. You can do so effortlessly with this command:

php artisan ladder:install

Lastly, execute the migration command to create a single pivot user_role table, assigning roles to users.

php artisan migrate	

Usage

Before using Ladder add the HasRoles trait to your App\Models\User model. By doing so this trait will provide the necessary methods to manage roles and permissions.

use Ladder\HasRoles;
 
class User extends Authenticatable
{
    use HasRoles;
}

HasRoles trait in detail

// Access all of user's roles...
$user->roles : Illuminate\Database\Eloquent\Collection

// Determine if the user has the given role... 
$user->hasRole($role) : bool

// Access all permissions for a given role belonging to the user...
$user->rolePermissions($role) : array

// Access all permissions belonging to the user...
$user->permissions() : Illuminate\Support\Collection

// Determine if the user role has a given permission...
$user->hasRolePermission($role, $permission) : bool

// Determine if the user has a given permission...
$user->hasPermission($permission) : bool

All method arguments can accept string, array, Collection, or Enum if desired. For optimal performance, it is advisable to use array or Collection as arguments when handling multiple entries.

Roles & Permissions

Users can receive roles with permissions defined in App\Providers\LadderServiceProvider using Ladder::role method. This involves specifying a role’s slug, name, permissions, and description. For instance, in a blog app, role definitions could be:

Ladder::role('admin', 'Administrator', [
    'post:read',
    'post:create',
    'post:update',
    'post:delete',
])->description('Administrator users can perform any action.');

Ladder::role('editor', 'Editor', [
    'post:read',
    'post:create',
    'post:update',
])->description('Editor users have the ability to read, create, and update posts.');

Assign Roles

You may assign roles to the user using the roles relationship that is provided by the Ladder\HasRoles trait:

use App\Models\User;

$user = User::find(1);

$user->roles()->updateOrCreate(['role' => 'admin']);

Authorization

For request authorization, utilize the Ladder\HasRoles trait’s hasPermission method to check the user’s role permissions. Generally, verifying granular permissions is more important than roles. Roles group permissions and are mainly for presentation. Use the hasPermission method within authorization policies.

/**
 * Determine whether the user can update a post.
 */
public function update(User $user, Post $post): bool
{
    return $user->hasPermission('post:update');
}

For more details, please visit Github.

Closing Note:

We are highly results-driven experts having experience in the grounds of Laravel web application development, CRM software, SAAS applications, and eCommerce web apps. So If you want to develop a custom web app you can contact us or Start a project with us.

Published at : 19-09-2023

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project