in many to many join i get this error :
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
laravel.posts_tag, CONSTRAINTposts_tag_tag_id_foreignFOREIGN KEY (Tag_ID) REFERENCESTag(ID) ON DELETE CASCADE) (SQL: insert intoPosts_Tag(Posts_ID,Tag_ID) values (26, Tag num 2))
my controller :
public function Share(Request $request)
{
$posts = Posts::create($request->all());
$posts->tags()->attach(Input::get('Tag'));
return $posts;
return Redirect()->back();
}
my model :
public function tags()
{
return $this->belongsToMany('App\Tag', 'Posts_Tag', 'Posts_ID', 'Tag_ID');
}
view :
{{ Form::open(array('url'=>'post')) }}
{{ Form::label('Title') }} : {{ Form::text('Title') }}
<br>
{{ Form::label('Content') }} : {{ Form::textarea('Content') }}
<br>
{{ Form::label('Tag :') }}
<select name="Tag" id="Tag" class="Tag">
@foreach($tag as $tag)
<option>{{ $tag->Tag }}</option>
@endforeach
</select>
<br>
{{ Form::submit('Share Post') }}
{{ Form::close() }}