【整理】Laravel 5 使用Entrust包如何注册用户时自动添加角色

原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】Laravel 5 使用Entrust包如何注册用户时自动添加角色

参考:Laravel 5 and Entrust. How to save user and attach role at the same time

通过Laravel的make:auth生成的AuthController.php中,直接修改下面代码即可:

public function create( array $data )
{
    // Create a new user, and assign it to 'new_user'
    $new_user = User::create( [
        'username'  => $data['username'], //<< Specific to my own db setup
        'email'     => $data['email'],
        'password'  => bcrypt( $data['password'] ),
    ] );

    // Initiate the 'member' Role
    $member = Role::where( 'name', '=', 'member' )->first();
    // Give each new user the role of 'member'
    $new_user->attachRole( $member );

    // Return the new user with member role attached
    return $new_user; //<<Or whatever you do next with your new user
}
原创文章,转载请注明: 转载自勤奋的小青蛙
本文链接地址: 【整理】Laravel 5 使用Entrust包如何注册用户时自动添加角色

文章的脚注信息由WordPress的wp-posturl插件自动生成



|2|left
打赏

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: