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

lifesound's avatar

how to know if user has permission ?

I already created users / roles many to many relationship. also, I have a roles/permissions many to many relationship . I could get user-> roles and role->permissions But how to know if the user hasPermission?

0 likes
10 replies
jlrdw's avatar

@lifesound have you looked over any of the free code on GitHub that covers authorization in detail. The code is from the free series here on laracasts.

2 likes
Snapey's avatar

@lifesound create a function in the user model that returns all the user's permissions as an array.

You can use a hasManyThrough on the roles table to get the permissions.

Probably cache them per user also.

1 like
jlrdw's avatar

@lifesound the laravel 6 and 8 from scratch series. @jeffreyway has links to Github code.

But other series cover starter kit specific code, i.e., fortify. Just browse over the various lessons.

In laravel 6 from scratch there are several videos that apply specifically to authorization.

2 likes
Snapey's avatar

@lifesound User can have many permissions through the roles table so hasManyThrough on the User model

2 likes
lifesound's avatar

@Snapey public function permissions() { return $this->hasManyThrough(Permission::class, Role::class); }

this gives null although i have permissions .. it is many to many on both sides ..

Please or to participate in this conversation.