Skip to content

Instantly share code, notes, and snippets.

@revooms
Created August 7, 2018 15:03
Show Gist options
  • Select an option

  • Save revooms/30e4ea64e1235967c37956361cd398c5 to your computer and use it in GitHub Desktop.

Select an option

Save revooms/30e4ea64e1235967c37956361cd398c5 to your computer and use it in GitHub Desktop.
Blade pager
@php
$hasFirstSeperator = false;
$hasLastSeperator = false;
$visibleButtons = 9; // odd number!!!
$first = 1;
$last = $p->lastPage();
$current = $p->currentPage();
// Main
$half = floor($visibleButtons / 2);
$offSetFirst = $first + $visibleButtons - $half;
$offSetLast = $last - $visibleButtons + $half;
$from = $current - $half;
if ( $from > ($last - $visibleButtons )) {
$from = $last - $visibleButtons;
}
$from = ($from < 2) ? 2 : $from;
$to = $from + $visibleButtons - 1;
$to = $to >= $last - 1 ? $last - 1 : $to;
// Seperators?
if ( $current > $offSetFirst ) {
$hasFirstSeperator = true;
}
if ( $current < $offSetLast ) {
$hasLastSeperator = true;
}
@endphp
<ul class="pagination justify-content-center">
{{-- First item --}}
<li class="page-item{{ $current==1 ? ' active' : null }}">
<a class="page-link" href="?page=1">1</a>
</li>
{{-- first seperator --}}
@if ( $hasFirstSeperator )
<li class="page-item">
<a class="page-link">&hellip;</a>
</li>
@endif
{{-- Middle items --}}
@for($x = $from; $x <= $to; $x++)
<li class="page-item{{ $current==$x ? ' active' : null }}">
<a class="page-link" href="?page={{ $x }}">{{ $x }}</a>
</li>
@endfor
{{-- Last seperator --}}
@if ( $hasLastSeperator )
<li class="page-item">
<a class="page-link">&hellip;</a>
</li>
@endif
{{-- Last Item --}}
<li class="page-item{{ $current==$last ? ' active' : null }}">
<a class="page-link" href="?page={{ $p->lastPage() }}">{{ $p->lastPage() }}</a>
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment