dsgwdigu84950 2015-06-26 20:00
浏览 137
已采纳

Laravel 5 - 如何在用户注册时向多个表插入行

How would I insert rows to multiple tables when a user gets registered.

I have the basic Laravel user table. I also have a businesses table (id, business_name) and a usersbusinesses table (id, user_id, business_id)

First, I need to create (db insert) the user and get its generated id value.

Then, I need to create (db insert) the business and get its generated id value.

Finally, I need to create (db insert) a row in the usersbusinesses table using the values above (user_id and business_id)

I know I’m going to have to modify the app/Services/Registrar.php file, I just don’t know how to tackle this. Current code only inserts to users table:

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
public function create(array $data)
{
    return User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

Thanks Christian

  • 写回答

1条回答 默认 最新

  • doutizha7526 2015-06-26 21:55
    关注

    If you want to do more, it's as simple as adding to that create function you just pasted.

    /**
     * Create a new user instance after a valid registration and do more.
     *
     * @param  array  $data
     * @return User
     */
    public function create(array $data)
    {
        $user = User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    
        $business = Business::create([
            'name' => 'best business ever'
        ]);
    
        $business->owners()->sync([$user->id]);
    
        return $user;
    
    }
    

    For the second-to-last part with:

    $business->owners()->sync([$user->id]);
    

    You should read about Eloquent relationships here.

    Because you're putting together this many commands, I would suggest reading into Jobs/Commands for Laravel. They're mentioned in the Queues section of Laravel docs.

    Update on how to add the owners() method.

    Business.php model:

    /**
     * Owners of the business.
     * 
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function owners()
    {
        return $this->belongsToMany('App\User', 'userbusinesses');
    }
    

    Similarly in your Users.php model:

    /**
     * Business this person owns.
     * 
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function businesses()
    {
        return $this->belongsToMany('App\User', 'userbusinesses');
    }
    

    However, my recommendation would be to stay inline with standards and rename your pivot table to business_user, in which case you wouldn't need the second argument in belongsToMany.

    Also note that I changed the command for saving to sync. I didn't realize it was a many-to-many the first time.

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

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟