'Using spatie(laravel-permission) in Laravel 8

When Assigning permission for a role using seeder, in permissions table it will take guard_name as "web", but when adding permission through CRUD, it will take guard_name as "sanctum". At the time of assigning this new permission to Role it will give an error.

RoleTableSeeder.php:

public function run()
{
    DB::table('roles')->delete();
    $roles_json = File::get("database/seeders/data/roles.json");
    $perms_json = File::get("database/seeders/data/permissions.json");
    $roles_data = json_decode($roles_json);
    $perms_data = json_decode($perms_json);
    Log::info("Starting Roles Population...");
    foreach ($roles_data->roles as $role) {
        $rl = Role::create([
            'name' => $role->name,
            'role_desc' => $role->role_desc
        ]);

        $rol = Role::findOrFail($rl['id']);
        foreach ($perms_data->permissions as $perm) {
            $per = Permission::findOrFail($perm->id);
            $rol->givePermissionTo($per);
        }
        Log::info("Populating... " . $role->name );
    }
    Log::info("Roles Populated Successfully.");
}

RoleController:

//---Assign Permission to Role
public function PermissionToRole($request, $id) {
    $selectedRole = Role::find($id);
    $selectedRolePermissions = $selectedRole->getAllPermissions();

    $users = User::role($id)->get();

    //---Remove Selected Role To Permissions
    for ($i=0; $i < count($selectedRolePermissions); $i++) {
        $selectedRole->revokePermissionTo($selectedRolePermissions[$i]);
    }

    if ($request->input('option') != null)
    {
        for ($k=0; $k < count($request->input('option')); $k++)
        {
            //---Insert New Permission To Role
            $findPermission = Permission::find($request->input('option')[$k]);
            $selectedRole->givePermissionTo($findPermission);
            //---Remove Old Permissions From User & Assign New Permissions To Users
            for ($m = 0 ; $m < count($users) ; $m++) {
                for ($j = 0 ; $j < count($users[$m]->getDirectPermissions()) ; $j++) {
                    if ($findPermission->id == $users[$m]->getDirectPermissions()[$j]->id) {
                        $users[$m]->revokePermissionTo($users[$m]->getDirectPermissions()[$j]);
                    }
                }
            }
        }
        return back()->with('success', '');
    }
}//---End of Function PermissionToRole

How to get both guard_name as "web" not "sanctum" because when assigning this permission to Role I get an error: The Error when assigning permission to role

routes/web.php

Route::get('pertorole/{role_id}', 'Roles\RoleController@PerToRoleEdit')->name('role.pertoroleedit')->middleware('can:Assign Permission');
Route::patch('pertorole/{role_id}', 'Roles\RoleController@PerToRoleUpdate')->name('role.pertoroleupdate')->middleware('can:Assign Permission');

app/Http/Kernel.php

<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \App\Http\Middleware\TrustProxies::class,
    \Fruitcake\Cors\HandleCors::class,
    \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Laravel\Jetstream\Http\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\Localization::class,
    ],

    'api' => [
        'throttle:api',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
    'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
    'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];

}



Solution 1:[1]

add protected $guard_name = 'web'; to your User model. sanctum default guard is "web".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Negar Javadzadeh