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

AbdulBazith's avatar

how to differentiate teacher and admin in a school project to enter the data laravel?

iam working with a school project. i have two roles teacher and admin.

i have assigned a specific class specific section to a teacher

eg: Abdul->belongsto-> V std-> A section

so when abdul login to the application when he clicks student attendance the auth fetches abdul's class section and list all the student of that class only. that is abdul no need to choose the class or section. based on his login it fetches that specific class and section.

so the form doesn't have class or section control in the form. it doesn't ask the teacher which class or section. based in the authentication it fetches the class.

This works fine.

but whats my doubt is, in that school there is a role admin he is not assigned to any class or any section. but he also has permission to put attendance for student. so when he logins and when he clicks student attendance form for him it should ask which class and which section drop down in the form.

for this i need to create two forms and two functions for teacher and admin?? else is there any other solution??

and if a teacher belongs to more than one class, for that teacher the drop down should show the two classes so that teacher will choose that specific class and put attendance.

my doubt is about the forms and controllers?? i need to create each for each role??

the same doubt in student mark, student admission ...

0 likes
6 replies
jlrdw's avatar

Start with getting admin out of your mind forget all about the word admin. In one system an admin cannot do accounting for example.

Now set up proper authorization to determine what a logged-in user can or cannot do. The role has no significance as far as terminology whatsoever.

But what does matter is what can that person who is logged in with a certain role do or not do.

Bear in mind some people have more than one role.

So the trick is if a method requires a certain role, so let's say Bob logs in he has 2 roles assigned to him Chief scientist and janitor, and a method that needs to be acted upon requires the role janitor, Bob has one of these assigned roles, therefore Bob can do that method.

That is an extreme example, chief scientist and janitor but the example was to show you forget about the terminology worry about can the person with that role do that method.

Use Query scopes to fine tune who can do what.

And have you considered using Spatie. This has a quick Spatie example, and a scope example. https://gist.github.com/jimgwhit/ed44a6c81815804f1ab910ce9eb88d84

Apply your RBAC, the example is custom, but a scope would be similar whether using laravel or Spatie or custom. Is used to fine tune, otherwise Separate queries.

AbdulBazith's avatar

@jlrdw thank you for your response.

i understood your response.

ya iam using spatie role and permission

ya whether that person can do or not that is necessary.

say that Bob can be an admin and has permission to add student attendance.

but bob is not assigned to any class or section

so when bob comes to put attendance for him the form should show class and section drop down

But AbdulBazith is teacher and has permission to add student attendnace is assigned to V std A section. so when he logins it doesnt ask him to choose the class and section drop down. directly the V std class and A section students will be listed after immediate login. because he is the class teacher of that specific class.

do i need to make tow forms for different permissions?? else some other way in same form?

simply to say when class teacher logins, system understands this class this section so it directly fetches those students. but when some other who is not assigned to that class, like principal it should show the drop downs of all classes. because principal is not assigned to specific class. she can view, add or delete all class. so principal chooses specific class in the drop down and perform any action.

i think you understood my question..

do i confused you or iam misunderstood you reply??

Kindly dont hesitate and answer please

jlrdw's avatar

Yes I see what you mean about if principal. In a case like that you could have the logic to show the drop down if the role is admin or principal.

In the laravel 6 from scratch series Jeffrey demonstrates using a gate to show admin true.

it's a pretty good video that will handle what you need I think.

But it's just some if logic.

AbdulBazith's avatar

@jlrdw thank you.

ya u are right there may be gates.

but with spatie package itself we can know that logged user is admin or principal or teacher. but for the drop down i think i need to write condition in both view form and controller i think so

for the dropdown and controller store function

jlrdw's avatar
jlrdw
Best Answer
Level 75

i think i need to write condition in both view form and controller

You could, or use middleware, or pass a variable from controller to view holding a parameter whether it's an admin or not. Will still need some if logic, but not as much.

I know many times this is all done in one view. But for larger apps I prefer to have separate controller methods and views for admin. I try to stay away from using too much middleware. Just my preference.

1 like

Please or to participate in this conversation.