用户注册时分配角色 [英] Assigning the role while user registration

查看:67
本文介绍了用户注册时分配角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在laravel工作,并且正在使用spatie许可包并且我想在注册时为用户分配不同的角色,我正在使用单选按钮从用户(例如编辑器,作家,博客)中获取角色如何根据用户输入为用户分配不同的角色

I am working in laravel and i am using the spatie permission package and I want to assign the different role for the user while registration I am using the radio button to get the role from a user such as editor ,writer,blogger how to I assign the different role to user based on the user input

推荐答案

在Laravel Auth \ RegisterController 中,您可以修改 create()函数.如果您使用的是 Spatie 软件包,则此方法有效.

In Laravel Auth\RegisterController you can modify the create() function. This is valid if you are using Spatie package.

protected function create(array $data)
    {
        $user =  User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

          $user->assignRole('customer'); //assign role to user
          return $user;    
    }

这篇关于用户注册时分配角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆