Hello I want to filter my response by tags and categories in laravel 9. my code is :
$posts = Post::when($categoryId > 0, function($query) use ($categoryId) {
return $query->where('category_id', '=', $categoryId);
})
->when($tagId > 0, function($query) use ($tagId){
return $query->whereHas('tags', function($query) use ($tagId) {
return $query->where('id', $tagId);
});
})
->get();
return view('blog::posts.index', compact('posts', 'categories'));
but i got this error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
select * from `posts` where exists (select * from `tags` inner join `post_tag` on `tags`.`id` = `post_tag`.`tag_id` where `posts`.`id` = `post_tag`.`post_id` and `id` = 4)