在Laravel中进行分页的过滤和排序

13 浏览
0 Comments

在Laravel中进行分页的过滤和排序

嗨,我正在使用Laravel,并且我有一个排序和过滤系统,它通过URL工作,像这样:

http://localhost:8000/halehule/category/103?type=all&minprice=+10+&maxprice=+10000000000+&color=&sortBy%5Bfield%5D=price&sortBy%5BorderBy%5D=desc

所以当我使用分页时,它不起作用并刷新到原始页面,像这样:

http://localhost:8000/halehule/category/103?page=2

我如何使用排序和过滤进行分页?

这是我的方法:

public function brandProduct($shop, $id, Request $request) {
  $colors = Color::all();
  $shop = Shop::where('english_name', $shop)->first();
  $shopTags = $shop->tags;
  $shopCategories = $shop->ProductCategories()->get();
  $categories = Shop::where('english_name', $shop->english_name)->first()->ProductCategories()->get()->where('parent_id', null);
  $brand = Brand::where('id', $id)->get()->first();
  $brands = $shop->brands;
  $shopProducts = $shop->products;
  $minPriceProduct = $shopProducts->min('price');
  $maxPriceProduct = $shopProducts->max('price');
  //颜色产品和类别产品合并
  if($request->color == null){
    $colorAndBrandProducts = $brand->products->sortByDesc('created_at');
  }
  else{
    $colorProducts = Color::where('code', $request->color)->get()->first()->products;
    $brandProducts = $brand->products;
    $colorAndBrandProducts = collect();
    foreach($colorProducts->toBase()->merge($brandProducts)->groupBy('id') as $allProducts){
    if($allProducts->count() > 1){
      $colorAndBrandProducts[] = $allProducts;
          }
    }
  $colorAndBrandProducts = $colorAndBrandProducts->first();
  }
  if ($request->has('type') and $request->has('sortBy') and $request->has('minprice') and $request->has('maxprice') and $request->has('color')) {
    if($colorAndBrandProducts != null){
      $minPrice = $request->minprice;
      $maxPrice = $request->maxprice;
      $filterBy = $request->type;
      $sortBy = $request->sortBy['field'];
      $perPage = 16;
      if($shop->template->folderName == 2){
        $sortBy_array = explode('|', $request->sortBy['field']);
        $sortBy = $sortBy_array[0];
        $orderBy = $sortBy_array[1];
      }
      else{
        $orderBy = $request->sortBy['orderBy'];
      }
      if ($request->type == 'all') {
          if ($orderBy == 'desc') {
              $products = $colorAndBrandProducts->whereBetween('price', [$minPrice, $maxPrice])->sortByDesc($sortBy)->unique('id');
          } else {
              $products = $colorAndBrandProducts->whereBetween('price', [$minPrice, $maxPrice])->sortBy($sortBy)->unique('id');
          }
      } else {
          if ($orderBy == 'desc') {
              $products = $colorAndBrandProducts->where('type', $filterBy)->whereBetween('price', [$minPrice, $maxPrice])->sortByDesc($sortBy)->unique('id');
          } else {
              $products = $colorAndBrandProducts->where('type', $filterBy)->whereBetween('price', [$minPrice, $maxPrice])->sortBy($sortBy)->unique('id');
          }
      }
  }
  else{
    $products = collect();
  }
}
else {
      $products = $colorAndBrandProducts;
  }
  $total = $products->count();
  $perPage = 16; // 您想要显示多少个项目。
  $currentPage = request()->page; // 索引页面。
  $productsPaginate = new LengthAwarePaginator($products->forPage($currentPage, $perPage), $total, $perPage, $currentPage);
  $template_folderName = $shop->template->folderName;
  SEOTools::setTitle($shop->name . ' | ' . $brand->name);
  SEOTools::setDescription($shop->description);
  SEOTools::opengraph()->addProperty('type', 'website');
  return view("app.shop.$template_folderName.layouts.partials.products", compact('products','minPriceProduct', 'maxPriceProduct', 'shopCategories', 'brand', 'shop', 'categories', 'productsPaginate', 'brands', 'shopTags','colors'));
  }

我使用这个方法进行排序和过滤,并使用分页,效果很好,但没有排序和过滤。

0
0 Comments

问题出现的原因是在laravel中使用分页时,没有进行筛选和排序。解决方法是在分页的时候,使用`->append($_GET)`来对数据进行筛选和排序。

解决方法代码如下:

$items = DB::table('users')
            ->orderBy('name', 'asc')
            ->paginate(10)
            ->appends($_GET);

以上就是在laravel中使用分页进行筛选和排序的解决方法。

0