我正在用 laravel 建立一个网站。我使用这些模型:
邮政
国家标签
城市标签
类别标签
一个帖子可以有很多个countrytags
,citytags
反之亦然categorytags
。
我想按标签搜索帖子。
我使用这个功能:
public function blogsearchresults(Request $request)
{
$attributes=request()->validate([
'countrytag_id'=>'required_without_all:citytag_id,categorytag_id',
'citytag_id'=>'required_without_all:countrytag_id,categorytag_id',
'categorytag_id'=>'required_without_all:countrytag_id,citytag_id'
]);
$posts=Post::all();
if($request->has('countrytag_id')) {
$countryid=$attributes['countrytag_id'];
$posts =$posts->whereHas('countrytags', function ($query) use ($countryid){
$query->wherein('countrytag_id', $countryid);
});
}
if($request->has('citytag_id')) {
$cityid=$attributes['citytag_id'];
$posts=$posts->whereHas('citytags', function ($query2) use ($cityid){
$query2->wherein('citytag_id', $cityid);
});
}
if($request->has('categorytag_id')) {
$categoryid=$attributes['categorytag_id'];
$posts=$posts->whereHas('categorytags', function ($query3) use ($categoryid){
$query3->wherein('categorytag_id', $categoryid);
});
}
$posts=$posts->paginate();
return view('pages.blog.blogsearchresults', compact('posts'));
}
但我得到这个错误:
Method Illuminate\Database\Eloquent\Collection::whereHas does not exist.
你能帮我解决这个问题吗?谢谢
慕丝7291255