Search code examples
laravellaravel-5permissionsroleslaravel-permission

Laravel Spatie Permission Multi Models to Permission


I know how to assign permissions to user.

class User extends Authenticatable
{
   use HasRoles;
}

$user->givePermissionTo('edit articles');

In this case, in model_has_permissions table will be inserted like this.

permission_id=> $permission_id
model_type=> "App\Models\User"
model_id=> $user_id

I have Team Model and I want to give permission to Team too. This is what I did.

class Teamextends Authenticatable
{
   use HasRoles;
}

$team->givePermissionTo('edit articles');

I wanted this result,

permission_id=> $permission_id
model_type=> "App\Models\Team"
model_id=> $team_id

But it didn't work for me. Anyone has experience such as above things? Should I make another table for it? like team_has_permission?


Solution

  • I found solution!

    class Team extends Authenticatable
    {
       use HasRoles;
       protected $guard_name = 'web';
    }
    
    $team->givePermissionTo('edit articles');
    

    This worked well. In db table, it saved like this.

    permission_id=> $permission_id
    model_type=> "App\Models\Team"
    model_id=> $team_id
    

    Really awesome!