猿问

laravel,用不同的过滤器过滤多对多的关系

我正在用 laravel 建立一个网站。我使用这些模型:

  • 邮政

  • 国家标签

  • 城市标签

  • 类别标签

一个帖子可以有很多个countrytagscitytags反之亦然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.

你能帮我解决这个问题吗?谢谢


HUH函数
浏览 116回答 1
1回答

慕丝7291255

该方法all()返回一个集合,您不能在其上使用查询构建器方法。(同样当您查询构建时,您不需要一遍又一遍地分配值 $posts)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'        ]);        $postQuery = Post::query();        if($request->has('countrytag_id')) {            $countryid = $attributes['countrytag_id'];            $postQuery->whereHas('countrytags', function ($query) use ($countryid){            $query->wherein('countrytag_id', $countryid);             });        }        if($request->has('citytag_id')) {            $cityid = $attributes['citytag_id'];            $postQuery->whereHas('citytags', function ($query2) use ($cityid){            $query2->wherein('citytag_id', $cityid);             });        }        if($request->has('categorytag_id')) {            $categoryid = $attributes['categorytag_id'];            $postQuery->whereHas('categorytags', function ($query3) use ($categoryid){            $query3->wherein('categorytag_id', $categoryid);              });        }                $posts = $postQuery->paginate();        return view('pages.blog.blogsearchresults', compact('posts'));    }
随时随地看视频慕课网APP
我要回答