标签: laravel-permission

没有名为“admin”的角色。拉拉维尔

我使用这个包:

https://github.com/spatie/laravel-permission/tree/v2

代码 :

     $user=User::find(2);
    $user->assignRole('admin');
Run Code Online (Sandbox Code Playgroud)

当我将管理员角色分配给用户时,我正在处理此错误

没有名为admin.Spatie\Permission\Exceptions\RoleDoesNotExist 的角色

这是我在 auth.php 中的默认保护:

    <?php

return [

    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],
Run Code Online (Sandbox Code Playgroud)

这是我的角色表:

在此处输入图片说明

这是我的 role_has_permission 表

在此处输入图片说明

这是我的权限表:

在此处输入图片说明

laravel spatie laravel-permission

2
推荐指数
1
解决办法
4870
查看次数

Laravel spatie/laravel-permission @can 指令

我目前正在使用 Spatie 角色和权限库。我有多个警卫,分别是管理员和网络。我的管理员有权使用 Guard_name = admin 来“管理身份验证”。当我尝试运行下面的语法时,它返回 true。

current_user()->hasPermissionTo('manage authentication', 'admin')
Run Code Online (Sandbox Code Playgroud)

但是当我使用 @can 指令时,span 标签不会出现。

@can('manage authentication')
<span>Hello</span>
@endcan
Run Code Online (Sandbox Code Playgroud)

我尝试了这个,但跨度标签仍然没有出现。

@can('manage authentication', 'admin')
<span>Hello</span>
@endcan
Run Code Online (Sandbox Code Playgroud)

我做错了吗?

更新:我的用户模型中没有任何角色/权限相关的属性和功能

laravel laravel-permission

2
推荐指数
1
解决办法
2416
查看次数

如何使用 laravel spatie 包检查 if 语句中的多个权限条件?

我需要检查 if 语句中的条件以获得多个权限。它使用 laravel 的 spatie 包。我使用下面的代码,但它似乎不起作用。可以显示输出,但输出不正确。它不会过滤条件。

if (auth()->user()->can('View Consultant') || auth()->user()('View Administration') 
|| auth()->user()('View Accountant') || auth()->user()('All departments')) 
 {

        $itemregistrations = DB::table('itemregistrations')
                             ->where('categoryid','=', '1',',','2',',','3')
                             ->get();

        return view('profil.index', compact('itemregistrations'));

 } 
Run Code Online (Sandbox Code Playgroud)

代码是否正确?

条件是有权限的用户(查看顾问、查看行政、查看会计、所有部门)可以查看所有部门的顾问、行政、会计列表。

有权限的用户(仅查看顾问)只能查看顾问列表。

if-statement laravel-5 laravel-permission

1
推荐指数
1
解决办法
6678
查看次数

如何在 spatie/laravel-permission 中使用 UUID

我用作ramsey/uuid所有项目表的主键。如何禁用自动增量并允许rolespermissions键可填充?我已经设法在其他桌子上做到这一点,但我坚持使用这两个。

php laravel eloquent laravel-5 laravel-permission

1
推荐指数
1
解决办法
3385
查看次数

Spatie Laravel Jetstream 中的 id '#' 没有任何作用

我正在尝试创建一个使用 Jetstream(一个全页 Livewire 组件)的 Laravel 项目,并且我正在尝试使用spatie/laravel-permission来获取权限。我总是得到以下错误

Spatie\Permission\Exceptions\RoleDoesNotExist 没有 id 的角色2

我有三个角色

1=Super Admin 
2=Admin 
3=User
Run Code Online (Sandbox Code Playgroud)

谁能帮我解决这个问题?

用户 Livewire 组件

use Livewire\Component;
use App\Models\User;
use App\Models\Role;

class Users extends Component
{
    public $userId;
    public $userData;

    public function addRole()
    {
        $this->userData = User::find($this->userId);
        $this->userData->assignRole(2);
    }
}
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-permission laravel-livewire laravel-jetstream

1
推荐指数
1
解决办法
2490
查看次数

Laravel Spatie 权限 - 如何通过另一个模型更改默认模型“角色”

请问我如何通过我的项目(laravel 项目)中的另一个模型更改默认模型“角色”,其架构与“角色”相同;默认模型包。

config/permission.php配置文件中包含:

'models' => [

    /*
     * When using the "HasPermissions" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your permissions. Of course, it
     * is often just the "Permission" model but you may use whatever you like.
     *
     * The model you want to use as a Permission model needs to implement the
     * `Spatie\Permission\Contracts\Permission` contract.
     */

    'permission' => Spatie\Permission\Models\Permission::class,

    /*
     * When using the "HasRoles" trait …
Run Code Online (Sandbox Code Playgroud)

laravel laravel-permission

0
推荐指数
1
解决办法
5446
查看次数

调用未定义的方法 App\User::givePermissionTo()

我正在使用 laravel spatie 包来获取角色权限,但我遇到了此错误,这是我的代码

$user = User::find(1);
$user->givePermissionTo('manager_product_create');
Run Code Online (Sandbox Code Playgroud)

php laravel laravel-permission

0
推荐指数
1
解决办法
5252
查看次数