Looking to hire Laravel developers? Try LaraJobs

laravel-permission-migrations maintained by fronx

Description
Versioned, reversible permission migrations for spatie/laravel-permission, run once per team.
Author
Fronx Team
Last update
2026/11/29 16:28 (dev-main)
License
Downloads
0

Comments
comments powered by Disqus

Laravel Permission Migrations

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Versioned, reversible migrations for the permissions and roles of spatie/laravel-permission.

They work like database migrations, for authorization data: each one runs once, is recorded, and can be rolled back. With Spatie teams, each migration runs once per team, so a new release can give new permissions to the roles of every existing team, without reseeding and without touching the roles a team customized.

use App\Enums\Permission;
use App\Enums\Role;
use Fronx\PermissionMigrations\Migration;

return new class extends Migration
{
    protected ?string $group = 'products';

    public function up(): void
    {
        $this->create(Permission::ProductsView, Permission::ProductsUpdate)->for(Role::Owner);
    }

    public function down(): void
    {
        $this->drop(Permission::ProductsView, Permission::ProductsUpdate);
    }
};

Installation

Install the package with Composer:

composer require fronx/laravel-permission-migrations

Publish and run its migrations. They create the table that records which permission migrations ran, and add a nullable group column to the permissions table:

php artisan vendor:publish --tag="permission-migrations-migrations"
php artisan migrate

The group column is only needed if your migrations put permissions in groups, for example to show them by group on a roles screen. Without it, leave $group unset.

Publish the config file if you need to change its defaults:

php artisan vendor:publish --tag="permission-migrations-config"
return [
    'path' => database_path('permissions'),

    'table' => 'permission_migrations',

    'guard' => 'web',

    'role_enum' => null,
];
  • path: the folder of the permission migrations.
  • table: the table that records the migrations that ran.
  • guard: the guard of the permissions and roles the migrations create, unless a migration sets its own $guard.
  • role_enum: a backed enum of the roles every team must have. Its roles are created before the migrations run, even the ones no migration gives a permission to.

Writing permission migrations

Create a permission migration with:

php artisan make:permission-migration add_product_permissions --group=products

A migration has three helpers. Each one takes backed enums or strings:

  • create(...$permissions) registers permissions in the global catalogue, in the migration's $group. Creating a permission that already exists does nothing.
  • ->for(...$roles) gives the permissions just created to these roles of the current team, and creates the roles that are missing.
  • drop(...$permissions) takes permissions away from the roles and members of the current team, and removes them from the catalogue once no team uses them anymore. Rolling back one team leaves the other teams untouched.

group() puts the permissions of the next create() in another group than the migration's:

$this->group('invoices')->create(Permission::InvoicesView)->for(Role::Accountant, Role::Owner);

Teams

When Spatie teams are enabled (permission.teams), tell the package which teams exist, in a service provider:

use App\Models\Tenant;
use Fronx\PermissionMigrations\PermissionMigrations;

PermissionMigrations::resolveTeamsUsing(fn () => Tenant::pluck('id'));

Each team's migrations run with that team as the Spatie team. If your app needs its own context for a team, such as an initialized tenant, wrap the run with runInTeamUsing():

PermissionMigrations::runInTeamUsing(
    fn (string $tenantId, Closure $runAsTeam) => tenancy()->run(Tenant::find($tenantId), $runAsTeam),
);

To give a new team its permissions right away, migrate it when it is created, for example from a listener:

use Fronx\PermissionMigrations\PermissionMigrator;

app(PermissionMigrator::class)->migrate($tenant->id);

Without teams, every migration runs once for the whole app.

Commands

php artisan permission:migrate            # run the pending migrations of every team
php artisan permission:migrate --team=42  # only for one team
php artisan permission:migrate:status     # what ran and what is pending, per team
php artisan permission:rollback           # roll back the last batch of every team
php artisan permission:rollback --team=42 # only for one team
php artisan make:permission-migration add_product_permissions --group=products

Migrations and rollbacks run in a database transaction, and clear the Spatie permission cache when they finish.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.