Reputation: 27
I want to search a post that has a similar tag only with tags id number
I've tried this
$tags = Tag::find($id);
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('id',$id);})->paginate(5);
return view('tagdetail', compact('tags', 'post'));
but the output is SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (23000).
I've already search through the internet and still cant.
Tag.php
public function post()
{
return $this->belongsToMany('App\Post');
}
Post.php
public function tags()
{
return $this->belongsToMany('App\Tag');
}
controller
public function detailTag($id)
{
//i know this will get all the post with the tag id, but i want to paginate it too.
$tags = Tag::find($id);
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('id',$id);
})->paginate(5);
return view('tagdetail', compact('tags', 'post'));
}
The expected result: to show all post that has a similar tag with paginating.
Actual result : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous (23000)
Upvotes: 0
Views: 51
Reputation: 8178
I've had this issue many times myself. It's just asking you to clarify which table you are trying to compare the id within, posts
or tags
, since both tables have the same key (id
). Try this, I think it will solve your issue:
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('tags.id',$id); // ** call out the tags table specifically
})->paginate(5);
Upvotes: 0