And why are you getting the error ?
Error SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
Here is my create post form
{!! Form::open(['route' => 'news.store', 'files'=>true]) !!}
<!-- {!! Form::hidden('User_id', Auth::user()->id) !!} -->
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<form role="form">
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'body:') !!}
{!! Form::textarea('body', null, ['class'=>'ckeditor', 'rows'=>5] ) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Choose an Image') !!}
{!! Form::file('image') !!}
</div>
<div class="form-group">
{!! Form::label('tags', 'tags:') !!}
{!! Form::select('tags[]', $tags, null, ['class'=>'form-control', 'multiple']) !!}
</div>
<div class="checkbox">
<label>
<input type="checkbox" /> I Accept
</label>
</div>
<div class="form-group">
{!! Form::submit('Save', array( 'class'=>'btn btn-danger form-control' )) !!}
here is my controller
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required|max:255',
'body' => 'required|max:100000',
'image' => 'image',
]);
$news = new news;
$news->tags()->attach($request->input('tags'));
$news->title = $request->title;
$news->body = $request->body;
if($request->hasFile('image')) {
$file = Input::file('image');
//getting timestamp
$timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
$name = $timestamp. '-' .$file->getClientOriginalName();
$news->image = $name;
$file->move(public_path().'/../public_html/news_image/', $name);
$thumb = Image::make(public_path().'/../public_html/news_image/' . $name)->resize(484, 300)- >save(public_path().'/../public_html/news_image/thumb/thumb' . $name, 50);
}
$news->save();
return redirect()->route('news.index');
}
this is my database migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNewsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('news', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->text('body');
$table->string('image')->default('defaulttagimage.jpg');
$table->timestamps();
$table->foreign( 'user_id' )->references( 'id' )->on( 'users' );
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('news');
}
}
Getting error when i create new post with tags
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (latest.news_tag, CONSTRAINT news_tag_news_id_foreign FOREIGN KEY (news_id) REFERENCES news (id) ON DELETE CASCADE) (SQL: insert into news_tag (news_id, tag_id) values (, 3), (, 4))
Nobody noticed the missing news_id in the insert statement?
insert into news_tag (news_id, tag_id) values (, 3), (, 4)
Instead of
$news = new News;
you should create it directly to get a news_id from the database (which is needed for the related news_tags table), otherwise your $news->news_id will be empty (which is the case in this one).
This way here might one right way:
// first CREATE the news (not just an empty instance, but a full model with own unique id)
$news = News::create([
'title' => $request['title'], // $request->title also works?
'body' => $request['body'], // $request->body also works?
'user_id' => Auth::user()->id // there might be a better solution, but this works 100%
]);
// since image has a default, you should be able leave it out at creation...I'm not sure.
// now your news has an ID and you can use the relationships...
$news->tags()->attach($request->input('tags'));
// route to anywhere...
return redirect()->route('news.index');
Please or to participate in this conversation.