【问题标题】:How to filter a collection in Laravel 5.5 based on multiple parameters?如何根据多个参数过滤 Laravel 5.5 中的集合?
【发布时间】:2018-02-05 18:30:11
【问题描述】:

我有一个 Profiles 模型,它有一些关系(belongsTo、HasManyThought)。

我想根据几个参数,一个接一个地过滤。

例如:

  • 选择输入以选择类别(hasManyThough 关系)。
  • 选择输入以选择位置。
  • 一个文本输入,用于键入几个单词并搜索我的模型(和关系)。

个人资料模型:

    class Perfis extends Model
{
    protected $guarded = [];
    public function usuario(){
        return $this->belongsTo(User::class, 'user_id');
    }
    public function enderecos(){
        return $this->belongsTo (Enderecos::class);
    }

    public function categorias(){
        return $this->belongsToMany (

            Categorias::class,
            'perfis_categorias'
        );
    }

    // Filtros
    public function scopeFilter($query, $filtros){

        return $filtros->apply($query);

    }


}

个人资料控制器

公共函数索引(PerfisFiltros $filtros, Request $request) { $perfis = Perfis::filter($filtros)->get();

    return view('frontal.perfis.index', [
        'perfis' => $perfis,
    ]);

}

我的过滤器

 public function apply($builder)
{
    if(! $this->request->has('_token')) return $builder;

    if($this->request->has('_token')){

            return $builder


            ->when($this->request->has('fCat'), function ($fCategoria) {
                $fCategoria->whereHas('categorias', function ($fCategoriaHas){
                    $fCategoriaHas->where('categorias_id', $this->request->fCat);
                });
            })
            ->when($this->request->has('fEstado'), function ($fEstado){
                $fEstado->whereHas('enderecos', function ($fEstadoHas){
                   $fEstadoHas->where('estados_id', $this->request->fEstado);
                });
            });
    }


}

【问题讨论】:

  • 那么,有什么问题?
  • 问题是我只能通过一个参数过滤。

标签: php laravel eloquent builder


【解决方案1】:

我无法理解你正在尝试做的事情......但我马上看到你回来得太早了......在这里

if(! $this->request->has('_token')) return $builder;

还有

if($this->request->has('_token')){

return $builder

你没有给这些家伙跑的机会:

->when($this->request->has('fCat'), function ($fCategoria) {
                $fCategoria->whereHas('categorias', function ($fCategoriaHas){
                    $fCategoriaHas->where('categorias_id', $this->request->fCat);
                });
            })
            ->when($this->request->has('fEstado'), function ($fEstado){
                $fEstado->whereHas('enderecos', function ($fEstadoHas){
                   $fEstadoHas->where('estados_id', $this->request->fEstado);
                });
            });

【讨论】:

  • 第一个“IF”语句是否定的。如果请求没有_token,它将返回不带参数的$builder
【解决方案2】:

我能够通过稍微改变我的查询来解决我的需求。

我的过滤器

if(!empty($this->request->fEstado)){
                $builder
                    ->whereHas('enderecos', function ($fEstadoHas){
                           $fEstadoHas->where('estados_id', $this->request->fEstado);
                        });
        }

        if(!empty($this->request->fCat)){
            $builder->whereHas('categorias', function ($fCat){
               $fCat->where('categorias_id', $this->request->fCat);
            });
        }


        if(!empty($this->request->fChave)){
            $builder->whereHas('anuncios', function ($fChave){
                $fChave->where('corpo', 'like', '%'.$this->request->fChave.'%')
                ->orWhere('titulo', 'like', '%'.$this->request->fChave.'%');
            });
        }



        return $builder;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2020-04-09
    • 2018-10-20
    • 2018-04-16
    • 1970-01-01
    • 2019-06-26
    • 2020-11-07
    相关资源
    最近更新 更多