我想使用ajax过滤器进行分页,但是要使用两个不同的控制器

时间:2019-06-18 11:47:42

标签: laravel-5.4 php-5.6

我想通过调用ajax方法来过滤一些数据。页面显示数据包含由控制器分页的分页。但是,我的ajax数据来自另一个控制器,这给分页过滤后的数据带来了麻烦。

公共函数HarvesterFilter($ page = 0)     {

    $harvesters = DB::table('harvester')
                        ->select('harvester.id as havId','brand.id as brandId','brand.name as brandName','brand.slug as brandSlug','harvester.slug as havSlug','model_name','harvester.image as havImage','power_Source','cutterbar','epower','hprice','harvester.crop')
                        ->join('brand','brand.id','=','harvester.brand')
                        ->where('status',1)
                        ->get();

    if($this->request->ajax()){

                        if($this->request->has('brands') && $this->request->has('brands') !== null)
                        {
                            $brands = explode(",",$this->request->brands);
                            $harvesters = $harvesters->whereIn('harvester.brand',$brands);
                        }   

                        if($this->request->has('cuttingwidth') && $this->request->has('cuttingwidth') !== null)
                        {
                            $cuttingwidth = explode(",",$this->request->cuttingwidth);

                             if(count($cuttingwidth) == 1)
                             {
                                if($cuttingwidth[0] == 'lessthan10')
                                    $harvesters = $harvesters->whereBetween('cutterbar',[1,10]);
                                elseif($cuttingwidth[0] == 'greternthan10')
                                    $harvesters = $harvesters->whereBetween('cutterbar',[11,20]);
                            }
                            elseif(count($cuttingwidth) > 1)
                            {
                                    $harvesters = $harvesters->whereBetween('cutterbar',[1,20]);
                            }
                        }

                        if($this->request->has('power') && $this->request->has('power') !== null)
                        {
                            $self = explode(",",$this->request->power);
                            $harvesters = $harvesters->whereIn('power_Source',$self);

                        }
                        $harvesters = $harvesters->paginate(10, ['*'], 'page', $page);
                        return  view('pages.harvesterGrid')->with('harvesters',$harvesters);
        }

     else{
                        return  view('layouts.harvesterGrid')->with('harvesters',$harvesters);
         }
}

<div class="col-md-9 col-sm-12 col-xs-12">
            @if(Request::has('brands') || Request::has('cuttingwidth') || Request::has('power'))
               {{$harvesters->appends(['brands' => Request()->brands ,'cuttingwidth' =>Request()->cuttingwidth,'power' => Request()->power,])->links()}}
            @else
                {{$harvesters->links()}}
            @endif
        </div>

0 个答案:

没有答案