亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

laravel,用不同的過濾器過濾多對多的關系

laravel,用不同的過濾器過濾多對多的關系

PHP
HUH函數 2022-07-02 16:47:07
我正在用 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.你能幫我解決這個問題嗎?謝謝
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

該方法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'));


    }


查看完整回答
反對 回復 2022-07-02
  • 1 回答
  • 0 關注
  • 129 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號