Laravel-permission: Is there a correct way to delete a permission?

Created on 9 May 2020  ·  4Comments  ·  Source: spatie/laravel-permission

I've read through the documentation and searched the issues, however I haven't seen an obvious way to delete a permission.

Can I assume the correct way is;
Remove the permissions from the role
Remove the permissions from users
Delete the permission

Are there any other steps? Removing from cache or something? Use case is where permissions can be dynamically added and removed, and in some cases deleted.

Thanks for package it really makes handling permissions simple.

support

Most helpful comment

Roles and Permissions are Eloquent models, so you can simply call ->delete() if you want them removed from everywhere. The migrations have foreign key relations, so it will remove them from anywhere they've been assigned.

For cache-clearing, if you are also assigning other roles/permissions at the same time using the built-in methods as documented at https://docs.spatie.be/laravel-permission/v3/advanced-usage/cache/ then the cache will be reset automatically. You can manually clear the cache if you need to as well.

All 4 comments

Roles and Permissions are Eloquent models, so you can simply call ->delete() if you want them removed from everywhere. The migrations have foreign key relations, so it will remove them from anywhere they've been assigned.

For cache-clearing, if you are also assigning other roles/permissions at the same time using the built-in methods as documented at https://docs.spatie.be/laravel-permission/v3/advanced-usage/cache/ then the cache will be reset automatically. You can manually clear the cache if you need to as well.

Roles and Permissions are Eloquent models, so you can simply call ->delete() if you want them removed from everywhere. The migrations have foreign key relations, so it will remove them from anywhere they've been assigned.

For cache-clearing, if you are also assigning other roles/permissions at the same time using the built-in methods as documented at https://docs.spatie.be/laravel-permission/v3/advanced-usage/cache/ then the cache will be reset automatically. You can manually clear the cache if you need to as well.

Thank you. I knew they were models, just wanted to be sure there wasn't any magic that would bite me.

I'll manually clear the cache just to be safe

Thanks so much!

Update: if you're using the built-in Role and Permission models, it will clear the cache automatically on delete. This is because the models use this trait:

use Spatie\Permission\Traits\RefreshesPermissionCache;

If you've created your own models that don't extend the built-in models but instead only implement the contract, then you will want to add the trait to your models.

Thanks Chris! Appreciate the extra info 👍

On Thu, May 14, 2020, 5:57 PM Chris Brown notifications@github.com wrote:

Update: if you're using the built-in Role and Permission models, it will
clear the cache automatically on delete. This is because the models use
this trait:

use SpatiePermissionTraitsRefreshesPermissionCache;

If you've created your own models that don't extend the built-in models
but instead only implement the contract, then you will want to add the
trait to your models.


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/spatie/laravel-permission/issues/1471#issuecomment-628762208,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AA427CQGOVBICNMCS475LWLRRQPJFANCNFSM4M45FTLA
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

neoreids picture neoreids  ·  3Comments

Dreambox13 picture Dreambox13  ·  3Comments

devingray picture devingray  ·  3Comments

ionesculiviucristian picture ionesculiviucristian  ·  4Comments

tripex picture tripex  ·  3Comments