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

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.

  • 写回答

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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献