Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I update the config/permission.php file for TEAMS features


How do I update the config/permission.php file for TEAMS features


To update the `config/permission.php` file for enabling and configuring TEAMS features in the Spatie Laravel Permission package, follow these detailed steps:

1. Enable Teams Feature
In the `config/permission.php` file, set the `'teams'` option to `true`. This activates the teams permission functionality, which allows roles and permissions to be scoped per team.

php
   'teams' => true,
   

2. Set Custom Foreign Key for Teams (Optional)
If your teams table uses a foreign key other than the default `team_id`, specify it by setting the `'team_foreign_key'` option in the same config file. For example, if your foreign key is `branch_id`, update as follows:

php
   'team_foreign_key' => 'branch_id',
   

This customizes the foreign key used in the relationships for team scoping[1][2][3].

3. Migration Considerations
- If you are enabling teams before running the initial migrations, simply update the config and run your migrations.
- If you have already run migrations without teams enabled and want to add teams support, run the artisan command to create a migration that adds the necessary team fields:


     php artisan permission:setup-teams
     php artisan migrate
     

This updates your database schema to support team-based permissions[1][2].

4. Middleware to Set Active Team ID
To make the package aware of the current team context during requests, create a middleware that sets the active team ID using the helper function `setPermissionsTeamId()`. For example:

php
   namespace App\Http\Middleware;

   class TeamsPermission {
       public function handle($request, \Closure $next) {
           if (!empty(auth()->user())) {
               // Assume team_id is stored in session on login
               setPermissionsTeamId(session('team_id'));
           }
           return $next($request);
       }
   }
   

Register this middleware and ensure it runs early in the request lifecycle (before route model binding) by adding it to the `$middlewarePriority` array in `app/Http/Kernel.php`[1][2].

5. Handling Role and Permission Queries Across Teams
When switching the active team context dynamically (for example, when an admin views another team's roles), you must:
- Call `setPermissionsTeamId($new_team_id)` to switch the active team.
- Unset cached relations on the user or model to reload roles and permissions for the new team:

php
     $user->unsetRelation('roles')->unsetRelation('permissions');
     

This ensures queries reflect the correct team's permissions[1][2].

6. Assigning Roles on Team Creation
When creating a new team, you may want to assign roles such as "Super Admin" to specific users for that team. You can do this in your Team model's boot method:

php
   public static function boot() {
       parent::boot();

       static::created(function ($model) {
           $session_team_id = getPermissionsTeamId();
           setPermissionsTeamId($model->id);

           User::find('your_user_id')->assignRole('Super Admin');

           setPermissionsTeamId($session_team_id);
       });
   }
   

This ensures roles are assigned within the correct team context[1].

By following these steps and updating the `config/permission.php` file accordingly, you enable and configure the teams feature in the Spatie Laravel Permission package, allowing you to manage roles and permissions scoped to different teams effectively.

Citations:
[1] https://spatie.be/docs/laravel-permission/v6/basic-usage/teams-permissions
[2] https://github.com/spatie/laravel-permission/blob/main/docs/basic-usage/teams-permissions.md
[3] https://laracasts.com/discuss/channels/general-discussion/spatielaravel-permissions-teams-functionality?reply=893599
[4] https://support.microsoft.com/en-us/office/manage-team-settings-and-permissions-in-microsoft-teams-ce053b04-1b8e-4796-baa8-90dc427b3acc
[5] https://acquaintsoft.com/blog/laravel-permission-demo
[6] https://sharegate.com/blog/manage-microsoft-teams-permissions-complete-handbook
[7] https://kb.siue.edu/page.php?id=142784
[8] https://learn.microsoft.com/en-us/microsoftteams/app-permissions