首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在laravel中注册用户后,如何将用户添加到其他表中?

在laravel中注册用户后,如何将用户添加到其他表中?
EN

Stack Overflow用户
提问于 2019-05-29 23:09:53
回答 1查看 2.1K关注 0票数 0

我正在用Laravel 5.6开发一个电子商务项目,其中有两个表供用户使用:

用于存储密码和其他内容的

  1. Users and
  2. UserDetails表,用于存储用户地址、电话、城市、PIN码等。

Users表中注册存储凭据的用户时,我希望在UserDetails表中创建一列,其user_id与添加到Users表时生成的相同。

下面是我的迁移:

代码语言:javascript
复制
Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamp('email_verified_at')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });


Schema::create('user_details', function (Blueprint $table) {
            $table->increments('id');
            $table->string('phone')->nullable();
            $table->string('city')->nullable();
            $table->string('pincode')->nullable();
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->timestamps();
        });

我曾尝试在RegisterControllercreate函数中添加以下内容

代码语言:javascript
复制
protected function create(array $data)
    {   
        User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        UserDetails::create([
            'user_id' => Auth::user()->id
        ]);

        return;
    }

但它不起作用,并显示

Trying to get property 'id' of non-object"

帮我解决这个问题。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56363671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档