在laravel 5.8注册期间添加ROLE时,参数1传递给Illuminate \ Auth \ SessionGuard :: login()

时间:2019-09-01 16:18:17

标签: php laravel roles

我正在尝试在用户注册时添加默认角色。我添加了->assignRole(),但它给了我这个错误

enter image description here

这是我的创建功能

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。好吧,我以为就是这样。

有什么建议吗?预先感谢!

1 个答案:

答案 0 :(得分:3)

在创建用户后只需添加assignRole()方法即可。

$user = User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => Hash::make($data['password']),
]);
$user->assignRole('borrower');