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

wordxpression's avatar

Spatie Permissions with teams... how to set it up in Laravel 11?

I'm trying to set up Spatie Permissions with Teams, but I seem to miss some essential steps, I can't get it working.

Does anyone know a good and clear coding example how to set this up?

Setting up Permissions without the teams is no problem, works great. But as soon as I want to introduce teams I'm more or less clueless how to do it... and it does not help, that the documentation on teams still seems to refer to pre-11 versions of Laravel.

0 likes
6 replies
LaryAI's avatar
Level 58

To set up Spatie Permissions with Teams in Laravel 11, you'll need to follow these steps. Please note that the Spatie Permissions package may have updates, so always refer to the official documentation for the most accurate and up-to-date information.

  1. Install the Package: First, you need to install the Spatie Laravel Permission package via Composer.
composer require spatie/laravel-permission
  1. Publish the Migration: After installing the package, you should publish the migration with the --teams flag to include the necessary columns for team support.
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="teams-migrations"
  1. Run the Migration: Once the migration is published, run it to create the required tables.
php artisan migrate
  1. Configure the Models: You need to add the HasRoles trait to your User model and specify that you're using teams.
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    protected $guard_name = 'web'; // or whatever guard you want to use

    public function scopeCurrentTeam($query)
    {
        return $query->where('team_id', auth()->user()->current_team_id);
    }
}
  1. Set Up Teams: You'll need to create a Team model if you don't have one already. This model should use the HasRoles trait as well.
use Illuminate\Database\Eloquent\Model;
use Spatie\Permission\Traits\HasRoles;

class Team extends Model
{
    use HasRoles;

    protected $guard_name = 'web'; // or whatever guard you want to use
}
  1. Create Roles and Permissions: Now you can create roles and permissions with the team_id property.
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

$teamId = 1; // The id of the team

$role = Role::create(['name' => 'editor', 'team_id' => $teamId]);
$permission = Permission::create(['name' => 'edit articles', 'team_id' => $teamId]);

$role->givePermissionTo($permission);
  1. Assign Roles to Users: Assign the role to a user within a team context.
$user->assignRole('editor', $teamId);
  1. Middleware: If you want to use middleware to check permissions, you can do so by passing the team id.
Route::group(['middleware' => ['role:editor,' . $teamId]], function () {
    // your routes
});
  1. Blade Directives: In your Blade templates, you can check for roles and permissions within a team context.
@role('editor', $teamId)
    // The user has the editor role within the team context
@endrole

@can('edit articles', $teamId)
    // The user has permission to edit articles within the team context
@endcan

Remember to replace $teamId with the actual team ID you want to check against. This is a basic setup, and depending on your application's needs, you might need to adjust or extend this setup. Always refer to the official Spatie Permission documentation for the latest information and advanced usage.

wordxpression's avatar

The code above by Lary AI is an implementation of some other kind of (self made) 'Teams' functionality. Not using the Teams options as the packages offers.

Does anyone have an example how to use the Teams functionality in the Spatie package itself?

wordxpression's avatar

@amitsolanki24_ Thanks, I did so before I posted, but somehow I have the feeling something changed in the 'how to do it' for Laravel 11, or I simply am doing it wrong. But I have the feeling I do everything that the docs say, and I still can't get it working.

2 likes
Richee's avatar

Hi @wordxpression did you get this working? I have set up roles and permissions but the documentation for teams seems to assume knowledge I don't have. Any tips would be welcome. Thanks.

Please or to participate in this conversation.