我正在用Laravel 5.6开发一个电子商务项目,其中有两个表供用户使用:
用于存储密码和其他内容的
在Users表中注册存储凭据的用户时,我希望在UserDetails表中创建一列,其user_id与添加到Users表时生成的相同。
下面是我的迁移:
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();
});
我曾尝试在RegisterController的create函数中添加以下内容
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"
。
帮我解决这个问题。
https://stackoverflow.com/questions/56363671
复制相似问题