I have created two tables in laravel 5.2 one is called "users" and the other is called "artists_details" and they have a one to one relationship. the schema of the users table is as follows
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->boolean('admin')->nullable();
$table->boolean('manager')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
and the schema of the artists table is as follows
Schema::create('artists_details', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('user_id')->unsigned();
$table->string('artists_image_path');
$table->string('name');
$table->integer('phone_no');
$table->integer('passport');
$table->string('city');
$table->string('county');
$table->string('facebook_name');
$table->string('twitter_handle');
$table->string('email');
$table->string('alternative_email');
$table->string('website');
$table->text('biography');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('CASCADE');
});
I have indicated the relationship in the models as follows User model
public function artists_relation()
{
return $this->hasOne('App\artists_details_model');
}
and on the artists_details_model as follows
public function user_artist_details()
{
return $this->belongsTo('App\User');
}
The php code that handles the form submission in the controller is as follows
public function artists_details_store(Request $request)
{
$input = \Request::all();
$file = $request->file('file');
$name = time(). $file->getClientOriginalName();
$file->move('artists_image/photo', $name);
$artists_input = new artists_details_model;
$artists_input->artists_image_path = 'artists_image/photo/'. $name;
$artists_input->name = $input['name'];
$artists_input->phone_no = $input['phone_no'];
$artists_input->passport = $input['passport'];
$artists_input->city = $input['city'];
$artists_input->county = $input['county'];
$artists_input->facebook_name = $input['facebook_name'];
$artists_input->twitter_handle = $input['twitter_handle'];
$artists_input->email = $input['email'];
$artists_input->alternative_email = $input['alternative_email'];
$artists_input->website = $input['website'];
$artists_input->biography = $input['biography'];
$artists_input->save();
return redirect('create');
}
When i click on the submit button i get the following error message
QueryException in Connection.php line 669:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
I cant seem to see where i am going wrong or what seems to be the problem
For the table 'artists_details' you have mentioned as
$table->foreign('user_id') ->references('id') ->on('users')
So whenever you try to save the details in the 'artists_details' you need to provide the 'user_id' without which the information will not get saved.
Either you need to pass the 'UserID' as a hidden parameter or if the UserID is saved in the Session, then you need to retrieve it from the Session.
您似乎没有在表单中包含外键{user_id}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.