Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

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!