【问题标题】:Laravel 5 - Manual paginationLaravel 5 - 手动分页
【发布时间】:2015-01-28 13:50:18
【问题描述】:

Pagination::make() 方法在 Laravel 5 中的 Pagination 类中不再存在。

在 Laravel 5 中是否有使手动分页工作的解决方法?

【问题讨论】:

    标签: laravel pagination laravel-5 laravel-pagination


    【解决方案1】:

    你需要添加使用:

    use Illuminate\Pagination\LengthAwarePaginator as Paginator;
    

    现在你可以使用了:

     $paginator = new Paginator($items, $count, $limit, $page, [
                'path'  => $this->request->url(),
                'query' => $this->request->query(),
            ]);
    

    获取与模型对象分页格式相同的数据;

    【讨论】:

    • 只是说(Laravel doc):When manually creating a paginator instance, you should manually "slice" the array of results you pass to the paginator.
    • 能解释一下参数吗?
    • 参数讨论过构造方法:laravel.com/api/5.0/Illuminate/Pagination/…
    • 那么如何在刀片前端渲染页面呢?谢谢
    【解决方案2】:

    Illuminate\Pagination\LengthAwarePaginator 使用示例:

    use Illuminate\Http\Request;
    use Illuminate\Pagination\LengthAwarePaginator;
    
    public function getItems(Request $request)
    {
        $items = []; // get array/collection data from somewhere
        $paginator = $this->getPaginator($request, $items);
    
        // now we can treat $paginator as an array/collection
        return view('some-view')->with('items', $paginator);
    }
    
    private function getPaginator(Request $request, $items)
    {
        $total = count($items); // total count of the set, this is necessary so the paginator will know the total pages to display
        $page = $request->page ?? 1; // get current page from the request, first page is null
        $perPage = 3; // how many items you want to display per page?
        $offset = ($page - 1) * $perPage; // get the offset, how many items need to be "skipped" on this page
        $items = array_slice($items, $offset, $perPage); // the array that we actually pass to the paginator is sliced
    
        return new LengthAwarePaginator($items, $total, $perPage, $page, [
            'path' => $request->url(),
            'query' => $request->query()
        ]);
    }
    

    然后在some-view.blade.php文件中,例如:

    @foreach($items as $item)
        {{--  --}}
    @endforeach
    
    
    {{ $items->links() }}
    

    https://laravel.com/docs/5.7/pagination#manually-creating-a-paginator

    【讨论】:

    • 我希望我能多次投票。这种示例应该添加到官方文档中。他们刚刚提到您可以手动创建分页,但没有显示如何。您的代码按预期工作,谢谢。
    【解决方案3】:

    你可以像这样创建手动分页

    $data = DB::table('post')->skip(0)->take(20)->get();

    【讨论】:

      【解决方案4】:

      实例化这个类的好方法

       use Illuminate\Pagination\LengthAwarePaginator as Paginator;
       //...
       $paginator = new Paginator($items->forPage($page, $limit), $count, $limit, $page, [
                  'path'  => Paginator::resolveCurrentPath()
              ]);
      

      注意 items 必须是 Collection 对象。使用collect(Array())Array 转换为Collection

      More informations

      【讨论】:

        【解决方案5】:

        试试下面的代码手动分页

        <?php
        
        namespace App\Http\Controllers;
        
        use Illuminate\Pagination\LengthAwarePaginator as Paginator;
        // use Illuminate\Pagination\Paginator;
        use Illuminate\Http\Request;
        use App\Product;
        class MyController extends Controller
        {
            public function index(Request $request){
                $items = Product::all();
        
                $filter_products = []; // Manual filter or your array for pagination
        
                foreach($items as $item){
                    if($item['id']>40 && $item['id']<50){
                        array_push($filter_products, $item);
                    }
                }
        
                $count = count($filter_products); // total product for pagination
                $page = $request->page; // current page for pagination
        
                // manually slice array of product to display on page
                $perPage = 5;
                $offset = ($page-1) * $perPage;
                $products = array_slice($filter_products, $offset, $perPage);
        
                // your pagination 
                $products = new Paginator($products, $count, $perPage, $page, ['path'  => $request->url(),'query' => $request->query(),]);
                // use {{ $products->appends($_GET)->links() }} to dispaly your pagination
                return view('index',['products' => $products]);
            }
        }
        

        【讨论】:

        • 为您的答案添加一些解释。仅代码的答案并不是很有用。
        【解决方案6】:
        public function myData($userid)
        {
            $data = static::get();
        
        
            $result = [];
            if(!empty($data)){
                foreach ($data as $key => $value) {
                    $result[$value->type.'-'.$value->postid][] = $value;
                }
            }
        
        
            $paginate = 10;
            $page = Input::get('page', 1);
        
        
            $offSet = ($page * $paginate) - $paginate;  
            $itemsForCurrentPage = array_slice($result, $offSet, $paginate, true);  
            $result = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($result), $paginate, $page);
            $result = $result->toArray();
            return $result;
        }
        

        【讨论】:

          【解决方案7】:

          基于@Avishay28,我认为不需要 php array_slice。

          在 laravel 8 中工作。

          我认为在性能方面这是一种更好的方法, 因为我们不是在查询“SELECT * FROM products

          $items_per_page = 5;
          $page_id = (int)$request->query('page') ?? 1;
          $model_docs_count = Product::count();
          $total_pages = ceil($model_docs_count / $items_per_page);
          $model = Product::skip(($page_id - 1) * $items_per_page)->limit($items_per_page)->latest();
          // dd($model->toSql()); "SELECT * FROM `products` WHERE ORDER BY `created_at` DESC LIMIT 5 OFFSET 0"
          
          $custom_paginate = new LengthAwarePaginator($model, $total_pages, $items_per_page, $page_id, [
              'path' => $request->url(),
              'query' => $request->query()
          ]);
          
          return response()->json([$custom_paginate], 200);
          

          【讨论】:

            【解决方案8】:

            另一种使用分页的方式是这样的:

            public function index()
            {
                $posts = DB::table('posts')->paginate(15);
            }
            

            【讨论】:

              猜你喜欢
              • 2015-05-28
              • 1970-01-01
              • 2016-01-26
              • 2016-10-23
              • 2017-11-30
              • 2015-05-09
              • 2019-01-26
              • 2016-01-18
              • 1970-01-01
              相关资源
              最近更新 更多