Laravel用户注册时创建userInfo数据库表 [英] Laravel Creating userInfo database table when user registers

查看:439
本文介绍了Laravel用户注册时创建userInfo数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对幼虫5的用户模型进行扩展构建。
我想要完成的是一旦用户注册并被验证,我希望创建一个名为userInfo的另一个数据库表有一个属性与用户模型的关系,用户模型与userInfo具有hasOne关系。



两件事。
如何成功实现这个逻辑。我正在阅读
http://laravel.com/docs/ master / eloquent-relationships#插入相关模型

http://laravel.com/docs/5.1/events#registering-events-and-listeners



但是我不是确定



其次。
最好地实现这个逻辑。



提前感谢



PS。由于用户模型是注册用户,而且在userInfo中存在的数据是可选,用户要填写认证后,我不会合并这两个数据库。

解决方案

如果我正确地理解了这个问题,你想在注册时向用户创建一个附加的相关模型。这应该是非常简单的,例如:

  $ user-> userInfo() - > save(UserInfo :: create ([
//任何需要保存到userInfo表的信息 - 如果有
]));

至于在哪里放置,您有选项。您可以将存储方法放在注册控制器上。或者将其提取给服务类RegisterUser,并在那里创建用户和用户信息。



如下:

  //控制器方法
public function store(Request $ request,RegisterUser $ registerUser)
{
$ user = $ registerUser-> handle ($请求);

return view(....)
}

// RegisterUser类
public function __construct(UserInfo $ userInfo)
{
$ this-> userInfo = $ userInfo;
}

public function handle($ request)
{
//创建您的用户
$ user = .....

$ user-> userInfo() - > save($ this-> userInfo-> create([
.....
]));

//任何你需要做的 - 发送电子邮件等

return $ user;
}






编辑:如果你是使用Laravel 5和默认的登录脚手架,那么你可以添加代码到app\Services\Registar类。该类由traRegister中的postRegister方法调用。更改创建方法:

  // app \Services\Registar 
public function create(array $ data)
{
$ user = User :: create([
'name'=> $ data ['name'],
'email'=> $ data [电子邮件'],
'password'=> bcrypt($ data ['password']),
]);

$ user-> userInfo() - > save($ this-> userInfo-> create([
.....
]));

//任何你需要做的 - 发送电子邮件等

return $ user;
}






如果您使用Laravel 5.1然后Registar类不再存在,create方法已被移动到AuthController(该方法是相同的,只是在不同的位置),所以覆盖它。


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.

解决方案

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.

这篇关于Laravel用户注册时创建userInfo数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆