It is complaining that wrong reference has been provided for the post ID. What are the values for $posts and $tag variables?
Dec 24, 2020
10
Level 9
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`study6_cms`.`post_tag`, CONSTRAINT `post_tag_post_id_foreign` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)) (SQL: insert into `po
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`study6_cms`.`post_tag`, CONSTRAINT `post_tag_post_id_foreign` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)) (SQL: insert into `post_tag` (`post_id`, `tag_id`) values (3, 3))
In my localhost post store with tag is properly working. But in server it throw an errror can anyone please tells me what I did wrong...? StoreMethod
public function store(PostStoreRequest $request)
{
$input = $request->validated();
$posts = auth()->user()->posts()->create($input);
if (request('tags') != '') {
$tags = explode(',', request('tags'));
foreach ($tags as $tag_name) {
$tag = Tag::firstOrCreate(['name' => $tag_name]);
}
$posts->tags()->attach($tag);
}
return redirect(route('posts.index'))->withMessage('post 😊 created successfully');
}
blade file
<input type="text" name="tags" class="form-control"
value="{{ old('tags') }}" />
@error('tags')
<small class="form-text text-red">{{ $message }}</small>
@enderror
Level 13
Check your migration order that may caused this problem.
2 likes
Please or to participate in this conversation.