doutang3077
2016-05-11 19:05
浏览 56
已采纳

Laravel创建多个用户表

I have two tables - users and user_attributes. During registration user I would like to not only enter the user in the user table but also create his attributes.

AuthController

    protected function create(array $data)
    {
        User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        return UserAttribute::create([
            'user_id' => $data['id'],  // Undefined index: id
            'lvl' => 0,
            'exp' => 0,
            'currency' => 500,
            'premium_currency' => 5,
        ]);
    }

How do I get user_id? PS: I'm sorry for my english.

图片转代码服务由CSDN问答提供 功能建议

我有两个表 - users和user_attributes。 注册用户我想不仅要输入用户 用户表,但也创建他的属性。

AuthController

  protected function create(array $ data)
 {
 User :: 创建([
'名称'=> $ data ['名称'],
'电子邮件'=> $ data ['email'],
'密码'=> bcrypt($ data ['密码 ']),
]); 
 
返回UserAttribute :: create([
'user_id'=> $ data ['id'],//未定义的索引:id 
'lvl'=>  0,
'ex'=> 0,
'currency'=> 500,
'premium_currency'=> 5,
 
)); 
} 
    
 
 

我如何获得user_id? PS:对不起我的英语。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougou8573 2016-05-11 19:21
    已采纳

    Do this $user =new User;
    $user->thing1 = 12; $user->thing2 =32; $user->save();

    And now as soon as you have called the save method you can get the id by doing

    $user->id; 
    

    Remember! It will only work after you have called the save method.

    Also remember to replace my data with your data.

    For further info and explanation see here: https://laravel.com/docs/5.2/eloquent#basic-inserts

    Best of luck!

    点赞 打赏 评论

相关推荐 更多相似问题