溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁功能

發(fā)布時(shí)間:2021-01-30 14:24:50 來源:億速云 閱讀:216 作者:Leah 欄目:開發(fā)技術(shù)

怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁功能?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

#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,
    ]);
}

從上面就可以看出,分頁的關(guān)鍵就在于LengthAwarePaginator。

LengthAwarePaginator的構(gòu)造方法。

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

其實(shí)已經(jīng)很明白了,假如要分頁的數(shù)組為

[
  ['username'=>'zhangsan', 'age'=>26],
  ['username'=>'lisi', 'age'=>23],
  ['username'=>'wangwu', 'age'=>62],
  ['username'=>'zhaoliu', 'age'=>46],
  ['username'=>'wangmazi', 'age'=>25],
  ['username'=>'lanzi', 'age'=>24],
  ['username'=>'pangzi', 'age'=>21],
]

共7條數(shù)據(jù),每頁顯示3條,共3頁

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Http\Request;
# 僅做演示 #
function userList(Request $request) {
  $users = [
    ['username'=>'zhangsan', 'age'=>26],
    ['username'=>'lisi', 'age'=>23],
    ['username'=>'wangwu', 'age'=>62],
    ['username'=>'zhaoliu', 'age'=>46],
    ['username'=>'wangmazi', 'age'=>25],
    ['username'=>'lanzi', 'age'=>24],
    ['username'=>'pangzi', 'age'=>21]
  ];
  $perPage = 3;
  if ($request->has('page')) {
      $current_page = $request->input('page');
      $current_page = $current_page <= 0 ? 1 :$current_page;
  } else {
      $current_page = 1;
  }
  $item = array_slice($users, ($current_page-1)*$perPage, $perPage); //注釋1
  $total = count($users);
  $paginator =new LengthAwarePaginator($item, $total, $perPage, $currentPage, [
      'path' => Paginator::resolveCurrentPath(), //注釋2
      'pageName' => 'page',
  ]);
  $userlist = $paginator->toArray()['data'];
  return view('userlist', compact('userlist', 'paginator'));
}

上面的代碼中的重點(diǎn)是$item,如果不做注釋1處理,得出的是所有7條數(shù)據(jù)。

注釋2處就是設(shè)定個(gè)要分頁的url地址。也可以手動(dòng)通過 $paginator ->setPath('路徑') 設(shè)置。

頁面中的分頁連接也是同樣的調(diào)用方式:

{{ $paginator->render() }}

好了,基本就是這樣,有紕漏的地方歡迎指正!

看看最終效果:

怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁功能

看完上述內(nèi)容,你們掌握怎么在Laravel中實(shí)現(xiàn)一個(gè)手動(dòng)分頁功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI