關於laravel 得手動分頁問題


      laravel 手動分頁,應用場景,需要在分頁數據中傳遞特殊參數,laravel自帶paginate方法不滿足的情況下。

   

  初始數據如下:

    $data = array(
      ['id'=>'1','user_id'=>2,'papaer_id'=>10],
      ['id'=>'2','user_id'=>2,'papaer_id'=>11],
    );

  轉為json的格式如下:

   [
    {
      "id": "1",
      "user_id": 2,
      "papaer_id": 10
    },
    {
      "id": "2",
      "user_id": 2,
      "papaer_id": 11
    }
  ]

這里一共2條數據,我們設定每頁顯示1個,一共2頁。

我們先看下laravel自帶方法,給我帶來的效果。

{
  "total": 2,
  "per_page": 1,
  "current_page": 1,
  "last_page": 2,
  "next_page_url": "http://127.0.0.1:8999/page?page=2",
  "prev_page_url": null,
  "from": 1,
  "to": 1,
  "data": [
    {
      "id": 1,
      "user_id": 2,
      "paper_id": 10
    }
  ]
}

我想在其中加自定義參數,比如這個路由的URL,已達到如下效果(根據自己所需加參數):

{

  “path”: "http://127.0.0.1:8999",
  "total": 2,
  "per_page": 1,
  "current_page": 1,
  "last_page": 2,
  "next_page_url": "http://127.0.0.1:8999/page?page=2",
  "prev_page_url": null,
  "from": 1,
  "to": 1,
  "data": [
    {
      "id": 1,
      "user_id": 2,
      "paper_id": 10
    }
  ]
}

 

首先我們看下laravel得分頁方法源碼:

#vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:480

public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
        $query = $this->toBase();

        $total = $query->getCountForPagination();

        $this->forPage(
            $page = $page ?: Paginator::resolveCurrentPage($pageName),
            $perPage = $perPage ?: $this->model->getPerPage()
        );

        return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => $pageName,
        ]);
}


我們發現這個關鍵就是用了lengthAwarePaginator

LengthAwarePaginator的構造方法,如下:

public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
{
        foreach ($options as $key => $value) {
            $this->{$key} = $value;
        }

        $this->total = $total;
        $this->perPage = $perPage;
        $this->lastPage = (int) ceil($total / $perPage);
        $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path;
        $this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage);
        $this->items = $items instanceof Collection ? $items : Collection::make($items);
}
 
        

控制器中,分頁方法調用:
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

  public function show(Request $request){
     $blogs = DB::table('blog')->where('uid',$uid)->select(); // 假設查出的數據
    
$perPage = 1; // 每頁顯示數量 if ($request->has('page')) { // 請求是第幾頁,如果沒有傳page數據,則默認為1 $current_page = $request->input('page'); $current_page = $current_page <= 0 ? 1 :$current_page; } else { $current_page = 1; }
$item = array_slice($real, ($current_page-1)*$perPage, $perPage); // 注釋1 $total = count($blogs); // 查詢總數 $paginator =new LengthAwarePaginator($item, $total, $perPage, $current_page, [ 'path' => Paginator::resolveCurrentPath(), // 注釋2 'pageName' => 'page', ]);

    return response()->json(['result'=>$paginator]) }
 
        

  注釋1: array_slice(array,start,length)   從start位置,去獲取length參數。結果返回一條數據

  注釋2:就是設定個要分頁的url地址。也可以手動通過 $paginator ->setPath(‘路徑’) 設置。

 

  頁面中的分頁連接也是同樣的調用方式 {{ $paginator->render() }}。

  注:返回得數據格式大概如下所示:

{

  “path”: "http://127.0.0.1:8999",
  "total": 2,
  "per_page": 1,
  "current_page": 1,
  "last_page": 2,
  "next_page_url": "http://127.0.0.1:8999/page?page=2",
  "prev_page_url": null,
  "from": 1,
  "to": 1,
  "data": [
    {
      "id": 1,
      "user_id": 2,
      "paper_id": 10
    }
  ]
}

 



如果,這篇文章幫到了你,歡迎點擊推薦。有疑問,請評論。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM