Laravel out of the box pagination handles this already.
Dec 28, 2020
12
Level 2
Redirect to current page or to 1st page when pagination is empty
So I want to redirect back to current page or to page 1 in case that the pagination does not exist so I can avoid that laravel can return empty string.
I'm a litt blocked here cause I don't have any solution in my mind how can I manage this in blade or elsewhere.
Any suggestion how to achieve this?
Thanks!
Level 2
I'm going to give another answer based on @newbie360 answer.
- As per Laravel 8 all you need to do is to export paginations views under
resources/viewsusing:
php artisan vendor:publish --tag=laravel-pagination
- After that the default pagination blade file as per laravel 8 is
bootstrap-4.blade.phpand on that view you can just do this:
@if ($paginator->hasPages())
<nav aria-label="Page navigation" class="page-navigation">
<span class="pagination-label">{{ __('pagination.page') }}</span>
{{-- Pagination Elements --}}
@foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
@if (is_string($element))
<li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li>
@endif
{{-- Array Of Links --}}
@if (is_array($element))
@foreach ($element as $page => $url)
@if ($page == $paginator->currentPage())
<form>
<div class="form-group">
<label for="page_number" class="sr-only">{{ __('pagination.page') }}</label>
<input
type="number"
class="form-control jump-to-page"
id="page_number"
name="page"
aria-describedby="pageNumber"
value="{{$page}}"
pattern="^[0-9]"
oninput="validity.valid||(value='')"
min="1"
step="1"
/>
</div>
</form>
@endif
@endforeach
<span class="pagination-total">{{ __('pagination.of') }} {{ $page }}</span>
@endif
@endforeach
<ul class="pagination">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
<span class="page-link" aria-hidden="true"><i aria-hidden="true" class="fas fa-chevron-left"></i></span>
</li>
@else
<li class="page-item">
<a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')"><i aria-hidden="true" class="fas fa-chevron-left"></i></a>
</li>
@endif
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li class="page-item">
<a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')"><i aria-hidden="true" class="fas fa-chevron-right"></i></a>
</li>
@else
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
<span class="page-link" aria-hidden="true"><i aria-hidden="true" class="fas fa-chevron-right"></i></span>
</li>
@endif
</ul>
</nav>
@endif
@if ($paginator->hasPages())
@once
@push('frontend.layouts.footer-scripts')
<script>
// jump-to-page
$(".jump-to-page").keydown(function(event){
if (event.which == 13){
var page = parseInt(this.value);
if (page >= 1 && page <= {{ $page }}) {
window.location.href='{{ implode("?", $paginator->getOptions()) }}='+page;
} else {
event.preventDefault();
}
}
});
</script>
@endpush
@endonce
@endif
As you can see I added event.preventDefault(); to prevent keydown in case that page number does not exist:
else {
event.preventDefault();
}
- Your controller could be simple as this:
public function categoryProducts($locale, $slug)
{
// dd($slug);
$category = Category::findOrFail($slug);
$products = Product::where('category_id', '=', $category->id)->paginate(9);
return view('frontend.pages.category_products', compact('category','products'));
}
Thanks again, I hope it can help someone!
1 like
Please or to participate in this conversation.
