Blogger Information
Blog 53
fans 3
comment 0
visits 55281
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
20200124--paginate()方法的封装与使用
邯郸易住宋至刚
Original
1185 people have browsed it

paginate()方法的封装与使用

一、paginate()封装

  1. //paginate()扩展
  2. QueryBuilder::macro('pages',function ($pagesize){
  3. $results = $this->paginate($pagesize);
  4. $items = $results->items();
  5. $results->lists = [];
  6. foreach ($items as $item){
  7. $results->lists[] = (array)$item;
  8. }
  9. return $results;
  10. });

二、paginate()封装后方法名称为pages()

  1. public function index(Request $request)
  2. {
  3. //接收前端传来的每页显示条数
  4. $limit = (int)$request->perpage;
  5. //如果前端不传值,给个默认值
  6. $limit = $limit>0?$limit:5;
  7. //使用自己封装的分页方法来得到分页结果
  8. $results = DB::table('xpcms_article')->pages($limit);
  9. //访问自己封装好的属性lists,得到前端循环的数组
  10. $data['articles'] = $results->lists;
  11. //调用links()并传参,参数是自己封装好的分页条视图文件
  12. $data['links'] = $results->links('public.paginate');
  13. return view('admins.content.index',$data);
  14. }

三、封装后,拿到的数据统一变为数组

在前端渲染时不用再多花心思去想要循环的数据是对象还是数组的问题了。

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments