doupi1532 2019-04-28 13:15
浏览 98
已采纳

分页不适用于过滤器。 点击第2页(带过滤器)将返回第2页,没有过滤器

I have problem with pagination. while using it with filter, only the first page is working.

Controller

public function create(Request $request)
{
    $products = Product::where(function($query) {
        //  filter category
        $category = Input::has('category')
            ? Input::get('category')
            : ['Clothes','Pants','Equipments','Shoes','Gears'];

        if (isset($category)) {
            $query->whereIn('category',$category);
        }

        // filter price
        $min_price = Input::has('min_price') ? Input::get('min_price') : 1 ;
        $max_price = Input::has('max_price') ? Input::get('max_price') : 999 ;
        if (isset($min_price) && isset($max_price)) {
            $query->where('price','>=',$min_price)
                ->where('price','<=',$max_price);
        }

        // filter brand
        $brand = Input::has('brand')
            ? Input::get('brand')
            : ['Adidas','Nike','Puma','Mizuno','Umbro','Line 7'];

        if (isset($brand)) {
            $query->whereIn('brand',$brand);
        }

        // filter store
        $store = Input::has('store')
            ? Input::get('store')
            : ['JD Sports Malaysia','SportsClick','Al-Ikhsan','Sports Wear','Sports Direct'];

        if (isset($store)) {
            $query->whereIn('store',$store);
        }

        //filter Gender
        $gender = Input::has('gender') 
            ? Input::get('gender') 
            : ['Male','Female','None'];

        if(isset($gender)){
            $query->whereIn('gender',$gender);
        }

        $search = Input::get('query');

        if (isset($search)) {
            $query->where('product','like',"%$search%")
                ->orWhere('brand', 'like', "%$search%")
                ->orWhere('category', 'like', "%$search%")->get();
        }
    })->paginate(12);

    return view('customer.category',compact('products'))->withProduct($products);
}

Blade

<!-- store bottom filter -->
<div class="store-filter clearfix mt-5">
    <span class="store-qty">Showing {{$products->count()}} Result(s)</span>
        <ul class="store-pagination" 
            {!! $products->links();!!}  
        </ul> 
    </div>
<!-- /store bottom filter -->

The controller code is how I get the filter input

and the view is how I display the pagination link

the page for number 2 and above should display the filtered product

  • 写回答

1条回答 默认 最新

  • doulu4976 2019-04-28 13:23
    关注

    Change $products->links(); to $products->appends($_GET)->links(), I think you will get the query parameters added in links.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器