Skip to main content

Permissions: Restrict Role Areas with Middleware

Premium
3 min read

Comments & Discussion

AA
Ali Al Qahtani ✓ Link copied!

In middleware I suggest using Enum class to get role ID like this:

middleware('role:' . \App\Enums\RoleEnum::TEACHER->value)

Readable

��
Денис Васильев ✓ Link copied!

What about creating 2 separate middleware? RoleSturen and RoleTeacher, is it a good idea? With certain check auth()->user()->role_id != 1 or auth()->user()->role_id != 2

PK
Povilas Korop ✓ Link copied!

Also possible, but then you would need to create more middlewares if you have new roles. Not so flexible.

ZS
Zhan Sundetov ✓ Link copied!

can u please make laravel 11

M
Modestas ✓ Link copied!

Hi, added to our to-do list!

PK
Povilas Korop ✓ Link copied!

Done, updated the course to Laravel 11 and to the text format!

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.