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 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果