Laravel 手动分页实现详解
2017-08-11 18:44
811 查看
因为Laravel 手册上对于手动分页的描述并不详细,这里大概说说笔者的实现方法
2
因为转换成数组的缘故,不能直接使用
2
以上的关键代码如下
然后是前端模板中的写法。Done!
查看更多,请访问我的博客
场景说明
在写一个大分类下所有文章的列表页时,笔者把取出来的数据都转换成了数组,这里为了清晰的说明并没有使用Repository来进行封装。/** * 当parent_id为0时为顶级分类,这时候会把顶级分类 * 下的文章以及子分类下的文章都放到一个数组中 * 当parent_id不为0时取出对应分类下的文章 */ Cate::find($id)->first()->parent_id == 0 ? $listInfo = array_merge(Cate::with('articles')->where('id',$id)->get()->toArray(), Cate::with('articles')->where('parent_id',$id)->get()->toArray()) : $listInfo = Cate::with('articles')->where('id',$id)->get()->toArray();1
2
因为转换成数组的缘故,不能直接使用
paginate()方法来实现分页。
手动实现分页
/** * ArticleListController.php * */ <?php namespace App\Http\Controllers; use App\Cate; use Illuminate\Pagination\LengthAwarePaginator; class ArtlistController extends AppController { public function index($id) { Cate::find($id)->first()->parent_id == 0 ? $listInfo = array_merge(Cate::with('articles')->where('id',$id)->get()->toArray(), Cate::with('articles')->where('parent_id',$id)->get()->toArray()) : $listInfo = Cate::with('articles')->where('id',$id)->get()->toArray(); $count = 0; foreach ($listInfo as $item) { $count += count($item['articles']); } $paginator = new LengthAwarePaginator($listInfo,$count,4); $categories = $this->parent(); return view('article.list',['categories' => $categories,'listInfo' => $listInfo,'paginator' => $paginator]); } }1
2
以上的关键代码如下
$count += count($item['articles'])把分页数据的
总数求出来
$paginator = new LengthAwarePaginator($listInfo,$count,4)这里的参数依次是
分页的数据,
分页数据的总数,
每页多少条。第三个参数可以直接写到conf文件中来灵活调整。
/** * list.blade.php * */ {!! $paginator->render() !!}
然后是前端模板中的写法。Done!
查看更多,请访问我的博客
by vampirebitter
相关文章推荐
- Laravel手动分页实现方法详解
- Laravel 手动分页实现详解
- Laravel 手动分页实现
- laravel手动创建数组分页的实现代码
- 利用Spring MVC+Mybatis实现Mysql分页数据查询的过程详解
- laravel 分页实现
- 详解用vue.js和laravel实现微信授权登陆
- Linux分页机制之分页机制的实现详解--Linux内存管理(八)
- PHP实现对数组分页处理实例详解
- ThinkPHP3.2.3实现分页的方法详解
- PHP和MYSQL实现分页导航思路详解
- Laravel实现autoload方法详解
- vuejs实现本地数据的筛选分页功能思路详解
- Yii框架分页实现方法详解
- Hibernate(十七)Hibernate实现分页和综合查询详解
- 用displaytag组件实现分页详解
- Laravel 5.3 auth中间件底层实现详解
- PHP长文章分页 实现手动分页代码 代码简单
- 举例详解用Java实现web分页功能的方法
- combogrid实现分页、动态搜索、过滤手动输入