Spatie许可没有任何权限命名为“ for Guard” Web'的权限。拉拉维尔

发布于 2025-02-13 07:17:27 字数 1296 浏览 1 评论 0原文

我正在使用Spatie权限,但它返回一个错误,

There is no permission named `create` for guard `web`.

我不知道为什么请帮忙 我的代码我在做什么是创建一个新用户,然后创建权限,然后我使用户讲师拥有我创建的权限,所有这些都在播种机中,

这是我的代码播种机

public function run()
{

    $user = User::create([
        'name' => 'admin',
        'email' => '[email protected]',
        'email_verified_at' => now(),
        'password' => 'password', // password
        'remember_token' => Str::random(10),
        'type' =>  User::TYPE_INSTRUCTOR,
    ]);

    $permissions = 
    [
    [
        'name' => 'methods_create',
        'display_name' => 'create',
        'key' => 'methods',
    ],
    [
        'name' => 'methods_update',
        'display_name' => 'update',
        'key' => 'methods',
    ],
    [
        'name' => 'methods_delete',
        'display_name' => 'delete',
        'key' => 'methods',
    ],
    ];

    

    $role = Role::create(['name'=>'Instructor']);
    foreach($permissions as $permission){
        Permission::create($permission);
    }
    $role->givePermissionTo($permissions);
    $user->assignRole($role);
}

i am using spatie permissions but it returns an error

There is no permission named `create` for guard `web`.

i don't know why please help
my code what am i doing is i create a new user then i create permissions then i make the user instructor have the permissions i create, all of that is in the seeder

here is my code seeder

public function run()
{

    $user = User::create([
        'name' => 'admin',
        'email' => '[email protected]',
        'email_verified_at' => now(),
        'password' => 'password', // password
        'remember_token' => Str::random(10),
        'type' =>  User::TYPE_INSTRUCTOR,
    ]);

    $permissions = 
    [
    [
        'name' => 'methods_create',
        'display_name' => 'create',
        'key' => 'methods',
    ],
    [
        'name' => 'methods_update',
        'display_name' => 'update',
        'key' => 'methods',
    ],
    [
        'name' => 'methods_delete',
        'display_name' => 'delete',
        'key' => 'methods',
    ],
    ];

    

    $role = Role::create(['name'=>'Instructor']);
    foreach($permissions as $permission){
        Permission::create($permission);
    }
    $role->givePermissionTo($permissions);
    $user->assignRole($role);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2025-02-20 07:17:27
 $permissions = $request->input('permission');

        $permissions = array_map(function ($item) {
          return (int)$item;
        }, $permissions);
           
        if (!empty($permissions)){
          $role->syncPermissions($permissions);
        }
 $permissions = $request->input('permission');

        $permissions = array_map(function ($item) {
          return (int)$item;
        }, $permissions);
           
        if (!empty($permissions)){
          $role->syncPermissions($permissions);
        }
天暗了我发光 2025-02-20 07:17:27
Previously my code was like that and i was getting the same error.

    $role->syncPermissions($request->permissions);


and then after googling so many hours and I found that synchPermissions() funcation expecting intiger value or array of integer value. So I have did like that and it is working fine.

    
    $numericPermissionArray = [];
    foreach($request->permissions as $permission) {
        $numericPermissionArray[] = intval($permission);
    }

    $role->syncPermissions($numericPermissionArray);
Previously my code was like that and i was getting the same error.

    $role->syncPermissions($request->permissions);


and then after googling so many hours and I found that synchPermissions() funcation expecting intiger value or array of integer value. So I have did like that and it is working fine.

    
    $numericPermissionArray = [];
    foreach($request->permissions as $permission) {
        $numericPermissionArray[] = intval($permission);
    }

    $role->syncPermissions($numericPermissionArray);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文