Ladder is a package by Enea Dhiamandi that provides feather-light permissions for Laravel. It simplifies role and permission management by avoiding storing everything in the database. Inspired by Jestream, it offers a static approach that reduces queries and ensures immutability for easy modifications.
Using the provided HasRoles
trait, you can access methods to manage user roles and permissions:
use Ladder\HasRoles; class User extends Authenticatable{ use HasRoles;}
// Determine if the user has a role$user->hasRole(string $role): bool // Permissions for a given role$user->rolePermissions(string $role): ?array // Determine if the user role has a given permission...$user->hasRolePermission(string $role, string $permission) : bool // Determine if the user has given permission...$user->hasPermission(string $permission) : bool
To manage roles, you can use the user model's roles()
association to create a role for the user. Then, you can check for granular permissions using the hasPermission()
method:
$user->roles()->updateOrCreate(['role' => 'admin']);$user->hasPermission('post:update');
To get started with this package in your Laravel project, you can install it via composer and artisan. It includes a database migration for roles, so you'll need to migrate the DB as well:
composer require eneadm/ladderphp artisan ladder:installphp artisan migrate
You can learn more about this package, get full installation instructions, and view the source code on GitHub.
Filed in:
Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.