我正在尝试在用户注册时添加默认角色。我添加了->assignRole()
,但它给了我这个错误
这是我的创建功能
use App\HasRoles;
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
])->assignRole('borrower');
$user_id = $user->id;
// HasRoles::create([
// 'role_id' => 4,
// 'user_id' => $users->id,
// ]);
Referral::find($data['referral_id'])->update ([
'status' => 1,
'date_used' => $data['referral_date_used']
]);
CollectorMember::create ([
'collector_id' => $data['referral_generate_by'],
'borrower_id' => $user_id,
'referral_id' => $data['referral_id'],
]);
return $user;
}
您还将注意到注释的hasRole :: create。好吧,我以为就是这样。
有什么建议吗?预先感谢!
答案 0 :(得分:3)
在创建用户后只需添加assignRole()
方法即可。
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
$user->assignRole('borrower');