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!

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度