gpt4 book ai didi

Laravel手动分页实现方法详解

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Laravel手动分页实现方法详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了Laravel手动分页实现方法。分享给大家供大家参考,具体如下:

这里的演示实例基于Laravel的5.2版本 。

在开发过程中有这么一种情况,你请求Java api获取信息,由于信息较多,需要分页显示。Laravel官方提供了一个简单的方式paginate($perPage),但是这种方法只适用model、查询构建器.

今天说下 给定一个数组如何实现 和paginate方法一样的效果.

查看paginate方法源码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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的构造方法.

?
1
2
3
4
5
6
7
8
9
10
11
12
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 );
}

其实已经很明白了,假如要分页的数组为 。

?
1
2
3
4
5
6
7
8
9
[
   [ '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条数据,每页显示3条,共3页 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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' ));
}

上面的代码中的重点是$item,如果不做注释1处理,得出的是所有7条数据.

注释2处就是设定个要分页的url地址。也可以手动通过 $paginator ->setPath('路径') 设置.

页面中的分页连接也是同样的调用方式:

?
1
{{ $paginator ->render() }}

好了,基本就是这样,有纰漏的地方欢迎指正! 。

看看最终效果:

Laravel手动分页实现方法详解

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助.

最后此篇关于Laravel手动分页实现方法详解的文章就讲到这里了,如果你想了解更多关于Laravel手动分页实现方法详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com