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

arcanaer's avatar

Laravel Permissions For Insitutions and Campuses

I'm building a Laravel application to manage schools and their campuses. The application needs to handle roles and permissions for users who can be institution administrators, campus administrators, teachers, students, etc. I am using the Spatie Laravel Permission package for roles and permissions, but I'm unsure of the best way to structure my relationships between schools, campuses, and users. What do you think is the best approach?

Solution 1: Use a single teams table (polymorphic relationship) for both institutions and campuses

One approach would be to use the teams table from Spatie Laravel Permission (which is a polymorphic table) to represent both institutions and campuses. The teamable polymorphic relationship can help distinguish between institutions and campuses.

Structure:

institutions table: Represents institutions.

campuses table: Represents campuses, and each campus is linked to an institution.

teams table: A polymorphic table that can represent either an institution or a campus, using columns like teamable_type and teamable_id (where teamable_type could be Institution or Campus and teamable_id points to the respective entity ID).

Users: Users are assigned to teams (either institutions or campuses). The model_has_roles table would reference the teams table via the polymorphic relationship.

Roles and permissions: Managed in the role_has_permissions table and model_has_roles, with the team_id functionality from Laravel Permission Spatie.

Solution 2: Use separate institution_user and campus_user tables for direct relationships in model_has_roles

Another approach would be to create specific pivot tables for users belonging to either institutions or campuses. These tables (institution_user and campus_user) would directly associate users with roles in the model_has_roles table.

Structure:

institutions table: Represents institutions.

campuses table: Represents campuses, and each campus is linked to an institution.

institution_user table: A pivot table linking users to institutions. This would have columns like user_id and institution_id.

campus_user table: A pivot table linking users to campuses. This would have columns like user_id and campus_id.

Roles and permissions: The model_has_roles table would link roles directly to users through the institution_user or campus_user tables by specifying the model_type as InstitutionUser or CampusUser (i.e., the pivot table name) and model_id as the corresponding institution_user or campus_user ID.

0 likes
1 reply
jlrdw's avatar

You could have prefix or suffix on a role.

Please or to participate in this conversation.