迁移pagination分页

仓库地址: pagination

安装

  1. composer require illuminate/pagination

我们可以用illuminate/pagination分页了

  1. $users = User::paginate(15);
  2. //在你的模板
  3. {!! $users->links() !!}

然后你将看到一堆莫名其妙的错误,没关系,让我们来解决它。既然不能像laravel那样使用,我们只好使用自定义分页

Model

  1. //Model.php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model as EloquentModel;
  4. use Core\Http\Request;
  5. class Model extends EloquentModel
  6. {
  7. public function scopePage($query ,$pageSize){
  8. $page = Request::getInstance()->getRequestParam('page') ? Request::getInstance()->getRequestParam('page') : 1;
  9. $paginator = $query->paginate($pageSize, ['*'],'page',$page);
  10. $paginator->setPath(\Core\Http\Request::getInstance()->getServerParams()['request_uri']);
  11. return $paginator;
  12. }
  13. }
  14. //User.php
  15. namespace App\Models;
  16. use Illuminate\Database\Eloquent\Model;
  17. use Laracasts\Presenter\PresentableTrait;
  18. class User extends Model
  19. {
  20. }

控制器

  1. // 在Index控制器类添加以下方法
  2. function index(){
  3. $user = User::page(5);
  4. //or
  5. //$user = User::where('name', 'like', "%{$keyword}%")->page(5);
  6. //$user->appends('keyword',$keyword);
  7. }

自定义函数(在Blade提到过)

  1. if (! function_exists('paginator')){
  2. /**
  3. * 自定义分页
  4. * @param string $paginator data
  5. * @return string
  6. */
  7. function paginator($paginator = ''){
  8. $request = \Core\Http\Request::getInstance();
  9. $page = $request->getRequestParam('page') ?: 1;
  10. $win = new \Illuminate\Pagination\UrlWindow($paginator);
  11. $url_arr = $win->get(3);
  12. $text = '<ul class="pagination">';
  13. if ($paginator->hasPages()) { //有结果集才显示啊
  14. if (!$paginator->onFirstPage()) {
  15. $text.="<li><a href=\"{$paginator->previousPageUrl()}\" rel=\"prev\" class=\"page-numbers previous\">上一页</a></li>";
  16. }
  17. if (isset( $url_arr['first'] )) {
  18. foreach ($url_arr['first'] as $k=> $v ) {
  19. if ($k == $paginator->currentPage()) {
  20. $style = "<span class=\"page-numbers current\">$k</span>";
  21. }else{
  22. $style = "<a href=\"{$v}\" class=\"page-numbers\">$k</a>";
  23. }
  24. $text.= "<li>$style</li>";
  25. }
  26. }
  27. if (isset( $url_arr['slider'] )) {
  28. foreach ($url_arr['slider'] as $k=> $v ) {
  29. if ($k == $paginator->currentPage()) {
  30. $style = "<span class=\"page-numbers current\">$k</span>";
  31. }else{
  32. $style = "<a href=\"{$v}\" class=\"page-numbers\">$k</a>";
  33. }
  34. $text.= "<li>$style</li>";
  35. }
  36. }else{
  37. if ($url_arr['last'])
  38. $text.= "<li class=\"disabled\"><span class=\"page-numbers\">...</span></li>";
  39. }
  40. if (isset( $url_arr['last'] )) {
  41. foreach ($url_arr['last'] as $k=> $v ) {
  42. if ($k == $paginator->currentPage()) {
  43. $style = "<span class=\"page-numbers current\">$k</span>";
  44. }else{
  45. $style = "<a href=\"{$v}\" class=\"page-numbers\">$k</a>";
  46. }
  47. $text.= "<li>$style</li>";
  48. }
  49. }
  50. if ($paginator->lastPage()!=$page) {
  51. $text.="<li><a href=\"{$paginator->nextPageUrl()}\" rel=\"prev\" class=\"page-numbers next\">下一页</a></li>";
  52. }
  53. }
  54. $text .= '</ul>';
  55. return $text;
  56. }
  57. }

模板

  1. //在你的模板
  2. {!! paginator($products) !!}

然后,分页大功告成!