问题描述
我知道这是一个普遍的问题,但不知道这里出了什么问题.如您所见,有一个//return $user
,它显示了一个有效的ID.在数据库中也进行了检查.
I know this is a common problem but don't know what's wrong here. As you can see there's this //return $user
and it shows an valid id. Checked that in database as well.
$user = new User;
$user->first_name = $data['first_name'];
$user->last_name = $data['last_name'];
$user->email = $data['email'];
$user->phone_no = $data['phone_no'];
$user->created_from = 'Web App';
$user->save();
// return $user;
Session::put('user_id',$user->id);
// return $user->id;
$address = new Address;
$address->user_id = $user->id;
$address->first_name = $data['receiver_first_name'];
$address->last_name = $data['receiver_last_name'];
$address->email = $data['receiver_email'];
$address->address_line_1 = $data['receiver_address_line_1'];
$address->address_line_2 = $data['receiver_address_line_2'];
$address->landmark = $data['receiver_landmark'];
$address->pincode = $data['receiver_pincode'];
$address->phone_no = $data['receiver_phone_no'];
$address->created_from = 'Web App';
$address->save();
以下是迁移:这是用户迁移
Here are the migrations:This is the users migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function($table){
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('phone_no', 20)->nullable();
$table->string('password')->nullable();
$table->string('remember_token', 100);
$table->date('date_of_birth')->nullable();
$table->string('created_from');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
地址
Addresses
public function up()
{
Schema::create('addresses', function($table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('address_line_1');
$table->string('address_line_2')->nullable();
$table->string('landmark')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->string('phone_no', 13);
$table->integer('pincode');
$table->string('created_from');
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users');
});
}
以下是错误的屏幕截图(如果有帮助的话).
Here's a screenshot of error if it helps.
推荐答案
根据错误消息,您的错误是由于在adresses.user_id中插入一个值(FK到src.id)引起的,而该值在src中不存在. ID.
在示例中,您尝试在adresses.user_id中插入29,检查SELECT id FROM src WHERE id=29
是否返回任何结果.如果没有,那是你的问题.
According to the error message, your error is caused by inserting a value in adresses.user_id (FK to src.id), which does not exist in src.id.
In the example, you try to insert 29 in adresses.user_id, check if SELECT id FROM src WHERE id=29
returns any result. If not, there is your problem.
这篇关于SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!