【问题标题】:Pagination Not Working With filter. Click page 2(with filter) will return to Page 2 with no filter分页不适用于过滤器。点击第2页(有过滤器)将返回没有过滤器的第2页
【发布时间】:2019-09-17 07:50:57
【问题描述】:

我的分页有问题。与过滤器一起使用时,只有第一页有效。

控制器

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

刀片

<!-- 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 -->

控制器代码是我获取过滤器输入的方式

视图是我显示分页链接的方式

2 号及以上的页面应显示过滤后的产品

【问题讨论】:

  • 您的分页链接似乎没有保留查询参数,这就是您没有得到过滤结果的原因。

标签: php laravel pagination append laravel-pagination


【解决方案1】:

把$products-&gt;links();改成$products-&gt;appends($_GET)-&gt;links(),我想你会得到链接中添加的查询参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2019-02-21
    • 1970-01-01
    • 2018-05-11
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多