Skip to main content

Permissions: Restrict Role Areas with Middleware

Premium
3 min read
Ali Al Qahtani avatar

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

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

Readable

👍 7
Денис Васильев avatar
Денис Васильев

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

👍 1
Povilas Korop avatar

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

Zhan Sundetov avatar

can u please make laravel 11

👍 1
Modestas avatar

Hi, added to our to-do list!

Povilas Korop avatar

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.