doulutian4843 2015-08-15 19:16
浏览 245
已采纳

Laravel在用户注册时创建userInfo数据库表

I am doing an extension build on the User model on larval 5. What I want to accomplish is once the user registers and is validated I want ANOTHER database table created named userInfo which has a belongsto Relationship with the User model and the User model has a hasOne relationship with userInfo.

Two things. How to I successfully implement this logic. I was reading http://laravel.com/docs/master/eloquent-relationships#inserting-related-models and http://laravel.com/docs/5.1/events#registering-events-and-listeners

But Im not to sure

And second. Where best do I implement this logic.

Thanks in advance

PS. I do not what to combine the two database because the user model is when they register and data that lives in userInfo is "optional" for the user to fill out After authentication.

  • 写回答

2条回答 默认 最新

  • dt97868 2015-08-16 02:34
    关注

    If I am understanding the question correctly, you want to create an additional related model to the user when they register. That should be pretty straightforward - something like:

    $user->userInfo()->save(UserInfo::create([
        //whatever information you need to save to the userInfo table - if any
    ]));
    

    as for where to put it, you have options there. You could put it the 'store' method on your registration controller. Or extract it out to a service class 'RegisterUser', and create the user and userInfo there.

    something like:

    //controller method
    public function store(Request $request, RegisterUser $registerUser)
    {
        $user = $registerUser->handle($request);
    
        return view(....)
    }
    
    //RegisterUser class
    public function __construct(UserInfo $userInfo)
    {
        $this->userInfo = $userInfo;
    }
    
    public function handle($request)
    {
        // create your user
        $user = .....
    
        $user->userInfo()->save($this->userInfo->create([
            .....
        ]));
    
        // whatever else you need to do - send email, etc
    
        return $user;
    }
    

    Edit: If you are using Laravel 5 and the default registration scaffolding, then you can add code to the app\Services\Registar class. This class is called by the postRegister method in the trait. Change the create method:

    // app\Services\Registar
    public function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    
        $user->userInfo()->save($this->userInfo->create([
            .....
        ]));
    
        // whatever else you need to do - send email, etc
    
        return $user;
    }
    

    If you are using Laravel 5.1 then the Registar class doesn't exist anymore, the create method has been moved to the AuthController (the method is the same, just in a different location) so override it there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集