• Reading time ~ 1 min
  • 22.08.2023

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/ladder
php artisan ladder:install
php artisan migrate

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297