Assigning the role while user registration

*爱你&永不变心* 提交于 2021-02-11 14:49:19

问题


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


回答1:


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;    
    }



回答2:


Take that user input and use assignRole function as described in the package readme file

Something like

public function someController(Request $request)
{
    ....
    $user->assignRole($request->input('role'));
    ...
}

assuming you have a form input (checkbox, radio, text) with name role




回答3:


I have finally found a way to attach the different role to the user based on the user selection

In my register create function

$role = $data['userType'];

    if ($role == 'User') {
        $user->assignRole('User');
    }elseif ($role =='Vendor') {
        $user->assignRole('Vendor');
    }  



回答4:


You can try the following code:

protected function create(array $data)
{

    

        $user=User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'user_name' => $data['user_name'],
            'role_name' => $data['role_name'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

    $role_a = $data['role_name'];
    if($role_a  == 'student') {
        $role = Roles::select('id')->where('admin_name', 'student')->first();
        $user->roles()->attach($role);
        return $user;
        }
    elseif ($role_a  == 'clg_admin'){
        $role=Roles::select('id')->where('admin_name','clg_admin')->first();
        $user->roles()->attach($role);
        return $user;
    }
    elseif ($role_a  == 'univ_admin'){
        $role=Roles::select('id')->where('admin_name','univ_admin')->first();
        $user->roles()->attach($role);
        return $user;
    }
    elseif ($role_a  == 'gov_admin'){
        $role=Roles::select('id')->where('admin_name','gov_admin')->first();
        $user->roles()->attach($role);
        return $user;
    }
    elseif ($role_a  == 'hod'){
        $role=Roles::select('id')->where('admin_name','hod')->first();
        $user->roles()->attach($role);
        return $user;
    }

}


来源:https://stackoverflow.com/questions/51617190/assigning-the-role-while-user-registration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!