多对多在哪里
Many to Many wherehas
我想搜索一个 post 具有类似标签但标签 ID 号
我试过了
$tags = Tag::find($id);
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('id',$id);})->paginate(5);
return view('tagdetail', compact('tags', 'post'));
但输出是 SQLSTATE[23000]:违反完整性约束:1052 列 'id' 在 where 子句中不明确 (23000)。
我已经在网上搜索过了,还是搜索不到。
Tag.php
public function post()
{
return $this->belongsToMany('App\Post');
}
Post.php
public function tags()
{
return $this->belongsToMany('App\Tag');
}
控制器
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'));
}
预期结果:显示所有post具有类似标签的分页。
实际结果:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列 'id' 不明确 (23000)
我自己也遇到过很多次这个问题。它只是要求您澄清您正在尝试比较哪个 table 中的 id,posts
或 tags
,因为两个 table 具有相同的键(id
).试试这个,我想它会解决你的问题:
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('tags.id',$id); // ** call out the tags table specifically
})->paginate(5);
我想搜索一个 post 具有类似标签但标签 ID 号
我试过了
$tags = Tag::find($id);
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('id',$id);})->paginate(5);
return view('tagdetail', compact('tags', 'post'));
但输出是 SQLSTATE[23000]:违反完整性约束:1052 列 'id' 在 where 子句中不明确 (23000)。
我已经在网上搜索过了,还是搜索不到。
Tag.php
public function post()
{
return $this->belongsToMany('App\Post');
}
Post.php
public function tags()
{
return $this->belongsToMany('App\Tag');
}
控制器
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'));
}
预期结果:显示所有post具有类似标签的分页。
实际结果:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列 'id' 不明确 (23000)
我自己也遇到过很多次这个问题。它只是要求您澄清您正在尝试比较哪个 table 中的 id,posts
或 tags
,因为两个 table 具有相同的键(id
).试试这个,我想它会解决你的问题:
$post = Post::whereHas('tags', function($q) use ($id){
$q->where('tags.id',$id); // ** call out the tags table specifically
})->paginate(5);