DekGenius.com
PHP
custom pagination laravel css
php artisan vendor:publish --tag=laravel-pagination
OR
use IlluminatePaginationPaginator;
public function boot()
{
Paginator::defaultView('your-pagination-view-name');
}
Laravel custom pagination
<?php
// config
$link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now)
?>
@if ($paginator->lastPage() > 1)
<ul class="pagination">
<li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
<a href="{{ $paginator->url(1) }}">First</a>
</li>
@for ($i = 1; $i <= $paginator->lastPage(); $i++)
<?php
$half_total_links = floor($link_limit / 2);
$from = $paginator->currentPage() - $half_total_links;
$to = $paginator->currentPage() + $half_total_links;
if ($paginator->currentPage() < $half_total_links) {
$to += $half_total_links - $paginator->currentPage();
}
if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) {
$from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1;
}
?>
@if ($from < $i && $i < $to)
<li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
<a href="{{ $paginator->url($i) }}">{{ $i }}</a>
</li>
@endif
@endfor
<li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
<a href="{{ $paginator->url($paginator->lastPage()) }}">Last</a>
</li>
</ul>
@endif
Customize laravel pagination links
{{ $users->onEachSide(5)->links() }}
custom pagination in laravel
'data' => [
'meta' => [
'totalPages' => ceil($pagination->totalRecords / $pagination->perPage),
'currentPage' => $pagination->currentPage + 1,
'totalRecords' => $pagination->totalRecords,
'recordsOnCurrentPage' => count($records),
'recordFrom' => ($pagination->currentPage * $pagination->perPage) + 1,
'recordTo' => ($pagination->currentPage * $pagination->perPage) + count($records),
],
'records' => $records,
laravel create pagination
<?php
namespace AppHelpers;
use IlluminateContainerContainer;
use IlluminatePaginationLengthAwarePaginator;
use IlluminatePaginationPaginator;
use IlluminateSupportCollection;
class PaginationHelper
{
public static function paginate(Collection $results, $showPerPage)
{
$pageNumber = Paginator::resolveCurrentPage('page');
$totalPageNumber = $results->count();
return self::paginator($results->forPage($pageNumber, $showPerPage), $totalPageNumber, $showPerPage, $pageNumber, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => 'page',
]);
}
/**
* Create a new length-aware paginator instance.
*
* @param IlluminateSupportCollection $items
* @param int $total
* @param int $perPage
* @param int $currentPage
* @param array $options
* @return IlluminatePaginationLengthAwarePaginator
*/
protected static function paginator($items, $total, $perPage, $currentPage, $options)
{
return Container::getInstance()->makeWith(LengthAwarePaginator::class, compact(
'items', 'total', 'perPage', 'currentPage', 'options'
));
}
}
laravel create pagination
"autoload": {
"files": [
"app/Helpers/PaginationHelper.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App": "app/"
}
},
laravel create pagination
Route::get('/test_collect_pagintae', function () {
$users = AppUser::get();
$showPerPage = 20;
$paginated = PaginationHelper::paginate($users, $showPerPage);
return $paginated;
});
© 2022 Copyright:
DekGenius.com