简体   繁体   English

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

[英]laravel, filtering many to many relations with different filter

I'm building a website with laravel.我正在用 Laravel 建立一个网站。 I use these models:我使用这些模型:

  • Post邮政
  • Countrytag国标
  • Citytag城市标签
  • Categorytag类别标签

A post can have many countrytags , citytags or categorytags and vice versa.一个帖子可以有很多countrytagscitytagscategorytags ,反之亦然。

I would like to search posts by tags.我想按标签搜索帖子。

I use this function:我使用这个功能:

    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'));

        }

But I get this error:但我收到此错误:

Method Illuminate\Database\Eloquent\Collection::whereHas does not exist.

Could you please help me in solving this issue?你能帮我解决这个问题吗? Thank you谢谢

the method all() returns a collection, you can't use query builder methods on it. all()方法返回一个集合,您不能在其上使用查询构建器方法。 (also when you query build, you dont need to assign the value $posts over and over) (同样,当您查询构建时,您不需要一遍又一遍地分配值 $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'));

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM