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

AbdulBazith's avatar

Having many roles, and permissions how to check condition in nav bar and blade files

Guys iam working with a project School Management System

i have a doubt in checking condition in roles and permissions.

I used spatie-laravel-permission package for multiple authentication.

Everything works fine, i have added, user, role, permission, role->permission, user->role

But say for example

i have role Teacher, and i have total 10 permissions,

so if the teacher role have 7 permissions, then in my nav bar i have 10 items means, for each option i need to write the check condition like below

@role('teacher')
    item 1
@role('teacher')
    option 2
@role('teacher')
    option 3
@role('teacher')
    option 4
.
.
.
so on i need to check like this?

and if i need to check more than one role means how it possible?? for each item i need to check every condition like below?

@role('teacher') or @role('admin') or @role('accountant') ... . .. . . so on  // like this i need to write many condition 
    item 1
@role('teacher')
    option 2

for teacher role i can write @role('teacher') item 1 , but my doubt is teacher, prinicipal, vice principal, coordinator, adminstrator. like this 5 roles can have permission to access the nav item1.

i need to write 'or' conditions, or have a simple way.

and another thing,

do i need to check with permission?? or role?? that is

if (role) or if (permission to this role). which condition is good

and i have a table for user_to_permissions. what is the necessity for that?? why i need to give permission to users directly. i will make a user to a role, and assign permission to that role.

Kindly reply for this please...

This is my permission list: https://imgur.com/GYQ133S

This is my role list: https://imgur.com/JyseLLT

Kindly reply please and suggest ideas.

If i need to wirte condition for each role then for each nav item, i have to write many conditions how it possibe? is that a right way??

0 likes
5 replies
eggplantSword's avatar

What you could do is create a user role table and set the id of the role in the user table as a foreign key, and create a base layout file and set the menu in there and with some ifs you can set the menu for each user role there and since it's in a base layout you only have to do it once. You can have a permissions table and link it to the role id not the user. This is a simple way that it could be done without having to add anything to your project like dependencies.

Snapey's avatar

you could split the menu into different @include files. So, have a teacher navbar and include if they have the role

@role('teacher')
    @include('teacherNavbar')
@endrole

@role('student')
    @include('studentNavbar')
@endrole
AbdulBazith's avatar

@msslgomez @snapey thank you for your response. everything worked fine based on your response

but i have two doubts.

  1. in my nav bar i cant use the role am i right?? because based on the permission only iam checking like below then here what is necessary for using roles. the same thing i will do in manage page, if can edit student then edit button will be showed else it will be hidden.
@can('Add Student')
show nav bar add student
@endcan

@can('Add Staff')
show nav bar add staff
@endcan

Kindly give me opinion

2nd doubt

Roles, Permissions, Staff, Student User table everything is right ok.

my user table

id  name    phno    password    type
1   AAA     123     ---         staff
2   BBB     321     ---         staff
3   CCC     547     ---         student

my class table

id      year_id     classname
1       1           V STD
2       1           VI STD
3       1           VII STD

my section table

id      year_id     class_id        sectionname
1       1           1           A section
2       1           1           B section
3       1           1           C section
4       1           2           A section

now whats my doubt is each staff will be a class teacher of a single class say for example Staff AAA will be class teacher of V std A section. then whenever staff AAA login, he/she mus only see that class info. they dont has permission to view add or delete other class, info such as attendance, mark, fee etc.

only those class teacher can see their class info. sometimes a staff can have more than one section at that time what should i do?

i know this is possible, for this i have to create a table staff_class by assigning each staff their respective class. am i right?? then how can i check the conditions?? Kindly suggest please please.

Snapey's avatar

You need a policy as well as permissions

For instance, a staff might have the ability to edit a class, but only their class

In a policy you can check permission AND the resource

Reference nav bar, yes wrap every single menu item in @can if necessary

Please or to participate in this conversation.