Write migrations for your laravel roles.
Adds migrations for
This package aims to provide migration functionality for roles and permissions (and their relationships).
Simply require this package with composer:
composer require ricardoboss/laravel-roles-migrations
This package adds a new abstract class -
RolesMigration - which you
can use to write migrations for your roles.
To add a new migration, simply execute
php artisan make:rolesmigration MyNewRolesMigration
to add a new migration (which can be found in
Open the migration. Notice how the class
extends the new
RolesMigration class instead
of the default
Migration class from Laravel.
Now comes the interesting part, writing the migrations.
A roles migration consists of four protected arrays which define what the migration does:
$permissions: contains permission definitions which shall be added
$roles: contains role definitions which shall be added
$toAttach: defines which permissions shall be attached to what role
$toDetach: defines which permissions shall be detached from what role
An example of each migration type and their outcome can be found here:
$permissions = [ [ 'name' => "List users", 'slug' => "users.list", // optional 'description' => "Permission to list all users." ] ];
slug value is optional.
It will be derived from the name if missing.
When the migration is executed, the above configuration will add a new permission to your database. This exact permission will be removed in case of a rollback.
$roles = [ [ 'name' => "Admin", 'level' => 10, 'slug' => "admin", // optional 'description' => "A user with all available permissions." // optional ] ];
description values are optional. If
slug is missing, it will
be derived from the name. The default value for
$toAttach = [ 'admin' => [ 'users.list', // more permissions... ] ];
$toDetach have the same structure.
They consist of nested arrays which declare what role gets or looses which permissions.
The key of the array must be the
slug of a role whereas the value must be an array
slugs. You can list as many permissions as you want for each role.
The order in which permissions are attached and detached matters: if you have the same configuration in both
$toDetach, the outcome will be that nothing changed since the permissions are first attached and then detached.
Feel free to fork the repository and create a pull request. You are encouraged to adhere to the PSR-12 coding style guide.
- Basic migrations and rollbacks
- support for updating existing roles/permissions
- write tests
If you see a missing feature you want or find a bug, please create an issue and describe it.
The source code of this package is free software and distributed under the terms of the MIT License.