Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mathewp's avatar

Laravel how to prevent deletion of a permission if it linked with any role

hi,

I am using spatie permission. I want to delete a 'permission' from permission table.

Before deleting, i want to check that whether this permission is linked with any 'role'. How is it possible.

And if it is linked with any role , i want to stop the deletion or

first delete the the permission from roles and then delete the permission.

0 likes
4 replies
BezhanSalleh's avatar

From the Docs

You can determine if a role has a certain permission:

$role->hasPermissionTo('edit articles');

A permission can be revoked from a role:

$role->revokePermissionTo('edit articles');
mathewp's avatar

thanks for the reply.

how can i implement it in a livewire component.. pls..

RanaSaddam's avatar

@BezhanSalleh I have checkboxes of permission, and I want to do that when I unchecked the particular checkbox then that permission gone removed from that role. Can anyone help me

mathewp's avatar

i have imported the role model in component

but

getting error

Method Illuminate\Database\Eloquent\Collection::hasPermissionTo does not exist.

Please or to participate in this conversation.