Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Pagination·ÖÒ³Êý¾Ý£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel pagination·ÖÒ³Êý¾Ý£¿
LaravelÊÇÒ»¸öÇ¿Ê¢µÄPHP¿ò¼Ü£¬¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¿ìËÙ¹¹½¨¸ßÖÊÁ¿µÄWebÓ¦ÓóÌÐò¡£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬¾³£ÐèÒª½«Êý¾Ý·ÖÒ³ÏÔʾ¡£LaravelÌṩÁËÒ»¸öÇ¿Ê¢µÄ·ÖÒ³Æ÷Àࡪ¡ªLaravel Pagination£¬Ê¹Êý¾Ý·ÖÒ³±äµÃºÜÊÇÈÝÒס£
ÔÚ±¾ÎÄÖУ¬ÎÒ½«ÏòÄúչʾÔõÑùʹÓÃLaravel PaginationÀ´·ÖÒ³Êý¾Ý¡£
Ò»¡¢×°ÖÃLaravel
ÔÚ×îÏÈʹÓÃLaravel Pagination֮ǰ£¬ÐèÒªÏÈ×°ÖÃLaravel¡£Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîͨ¹ýComposer×°ÖÃLaravel£º
composer create-project --prefer-dist laravel/laravel your-project-name
µÇ¼ºó¸´ÖÆ
¶þ¡¢½¨ÉèÄ£×ÓºÍÊý¾Ý¿âǨáãÎļþ
ÔÚ×îÏÈ·ÖÒ³Êý¾Ý֮ǰ£¬ÐèÒªÏÈ×¼±¸Ò»Ð©Êý¾Ý¡£ÔÚ±¾ÀýÖУ¬ÎÒÃǽ«Ê¹Óá°ÎÄÕ¡±Êý¾Ý¡£ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÃûΪ¡°Article¡±µÄÄ£×ÓºÍÒ»¸öÊý¾Ý¿âǨáãÎļþ¡£Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèËüÃÇ£º
php artisan make:model Article -m
µÇ¼ºó¸´ÖÆ
Ö´ÐдËÏÂÁîºó£¬½«ÌìÉúÒ»¸öÃûΪ¡°Article¡±µÄÄ£×ÓºÍÒ»¸öÃûΪ¡°create_articles_table¡±µÄǨáãÎļþ¡£·¿ªÇ¨áãÎļþ²¢½ç˵¡°articles¡±±íµÄ¼Ü¹¹¡£ÔÚ±¾Ê¾ÀýÖУ¬ÎÒÃǽ«Ê¹ÓÃÒÔÏ´úÂ룺
Schema::create('articles', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); });
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÒÔÏÂÏÂÁîÀ´ÔËÐÐǨá㣺
php artisan migrate
µÇ¼ºó¸´ÖÆ
Èý¡¢ÌìÉú²âÊÔÊý¾Ý
ÔÚÎÒÃÇ×îÏÈ·ÖÒ³Êý¾Ý֮ǰ£¬ÎÒÃÇÐèҪһЩ²âÊÔÊý¾Ý¡£Äú¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÌìÉúһЩ²âÊÔÊý¾Ý£º
factory(AppArticle::class, 50)->create();
µÇ¼ºó¸´ÖÆ
´Ë´úÂ뽫½¨Éè50Ìõ¡°ÎÄÕ¡±¼Í¼²¢½«ÆäÉúÑĵ½Êý¾Ý¿âÖС£
ËÄ¡¢·ÖÒ³Êý¾Ý
ÏÖÔÚ£¬ÎÒÃÇÒѾ׼±¸ºÃ×îÏÈ·ÖÒ³Êý¾ÝÁË¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravel PaginationÀàÇáËÉ·ÖÒ³Êý¾Ý¡£ÒÔÏÂÊÇÒ»¸öʾÀý¿ØÖÆÆ÷ÒªÁ죬Ëü½«´ÓÊý¾Ý¿âÖлñÈ¡¡°ÎÄÕ¡±Êý¾Ý²¢½«Æä·ÖÒ³£º
public function index() { $articles = Article::paginate(10); return view('articles.index', compact('articles')); }
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬ÎÒÃÇʹÓÃArticleÄ£×ÓµÄpaginateÒªÁì´ÓÊý¾Ý¿âÖлñÈ¡10¸ö¡°ÎÄÕ¡±¼Í¼£¬²¢½«Æäת´ï¸øÊÓͼ¡£paginateÒªÁ콫·µ»ØÒ»¸öPaginatorʵÀý£¬Ëü¾ßÓÐÒÔϳ£ÓÃÒªÁ죺
total£ºÊý¾Ý¼Í¼µÄ×ÜÊý¡£
perPage£ºÃ¿Ò³ÏÔʾµÄÊý¾Ý¼Í¼Êý¡£
currentPage£ºÄ¿½ñÒ³Âë¡£
lastPage£º×îºóÒ»Ò³µÄÒ³Âë¡£
nextPageUrl£ºÏÂÒ»Ò³µÄURL¡£
previousPageUrl£ºÉÏÒ»Ò³µÄURL¡£
ÔÚÊÓͼÖУ¬Äú¿ÉÒÔʹÓÃÒÔÏ´úÂëÇáËÉÉúÒòËØÒ³Á´½Ó£º
{{ $articles->links() }}
µÇ¼ºó¸´ÖÆ
Õ⽫ÌìÉú»ù±¾µÄ·ÖÒ³Á´½Ó£¬Äú»¹¿ÉÒÔʹÓÃÆäËûÑ¡Ïî×Ô½ç˵·ÖÒ³Á´½Ó¡£
Îå¡¢×Ô½ç˵·ÖÒ³Ñùʽ
ĬÈÏÇéÐÎÏ£¬LaravelÌìÉúµÄ·ÖÒ³Á´½ÓÏÔʾΪ¼òÆÓµÄÎı¾¡£ÈôÊÇÄúÏ£Íû×Ô½ç˵·ÖÒ³Á´½ÓµÄÑùʽ£¬¿ÉÒÔʹÓÃLaravelµÄBladeÄ£°åÒýÇæÇáËÉÌìÉú×Ô½ç˵HTML·ÖÒ³Á´½Ó¡£ÒÔÏÂÊÇÒ»¸öʾÀý£º
<div class="pagination"> <ul> {{-- Previous Page Link --}} @if ($paginator->onFirstPage()) <li class="disabled" aria-disabled="true"> <span>@lang('pagination.previous')</span> </li> @else <li> <a href="{{ $paginator->previousPageUrl() }}" rel="prev">@lang('pagination.previous')</a> </li> @endif {{-- Next Page Link --}} @if ($paginator->hasMorePages()) <li> <a href="{{ $paginator->nextPageUrl() }}" rel="next">@lang('pagination.next')</a> </li> @else <li class="disabled" aria-disabled="true"> <span>@lang('pagination.next')</span> </li> @endif </ul> </div>
µÇ¼ºó¸´ÖÆ
ÔÚ´ËʾÀýÖУ¬ÎÒÃÇʹÓÃLaravelµÄ@ifºÍ@endifÖ¸ÁîÓï·¨À´¼ì²éÄ¿½ñÊÇ·ñÔÚµÚÒ»Ò³ÉÏ£¬²¢ÌìÉú¡°ÉÏÒ»Ò³¡±°´Å¥¡£ÎÒÃÇ»¹Ê¹ÓÃ$paginator->nextPageUrl()ºÍ$paginator->previousPageUrl()ÒªÁìÌìÉú¡°ÏÂÒ»Ò³¡±ºÍ¡°ÉÏÒ»Ò³¡±°´Å¥µÄÁ´½Ó¡£
Áù¡¢×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÏòÄúÏÈÈÝÁËÔõÑùʹÓÃLaravel PaginationÀ´ÇáËÉ·ÖÒ³Êý¾Ý¡£ÎÒչʾÁËÔõÑùÌìÉúPaginatorʵÀý£¬ÔõÑùÏÔʾ»ù±¾µÄ·ÖÒ³Á´½ÓÒÔ¼°ÔõÑù×Ô½ç˵·ÖÒ³Á´½ÓµÄÑùʽ¡£Ï£ÍûÕâ¶ÔÄúÓÐËù×ÊÖú£¡
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Pagination·ÖÒ³Êý¾Ý£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡