phplaravellaravel-permission

spatie / laravel-permission can return always true


when using laravel spatie permissions every thing is ok when creating permissions , roles , assign roles to users. but when using $this->authorize('View Country') OR $user->can('View Country') always return true. i am using laravel v9.0 and spatie/permissions v5.5 and tried some solutions like :

but noting happen

assign role to user:

$user = Admin::findOrFail(request('user_id'));
        $role = Role::findOrFail(request('role_id'));
        $user->syncRoles([$role->name]);
        return redirect()->route('admin.admins.index')->with('success', __(" Successfully"));

create role permissions and sync

$role = Role::find(request('role_id', 1));
        foreach ($request->permissions as $permisssion) {
            $permisssions[] = Permission::firstOrCreate([
                'name' => $permisssion,
                'guard_name' => 'admin'
            ]);
        }
        $role->syncPermissions($permisssions ?? []);

Solution

  • I advise you to: