Reputation: 17383
I have a custom view and in some functions, I used paginate
and other functions I don't use paginate
. now how can I check if I used paginate
or not ?
@if($products->links())
{{$products->links()}}
@endif // not work
of course that I know I can use a variable as true false to will check it, But is there any native function to check it ?
Upvotes: 26
Views: 25960
Reputation: 191
If you use $products->paginate(); in the Controller, you will always have an instance of \Illuminate\Pagination\LengthAwarePaginator in the view.
If you want to check if the pagination is actually used:
@if($products->previousPageUrl() || $products->nextPageUrl())
{{ $products->links() }}
@endif
Upvotes: 0
Reputation: 326
check if products are instance of Pagination
@if($products instanceof \Illuminate\Pagination\LengthAwarePaginator)
{{ $products->links() }}
@endif
Upvotes: -1
Reputation: 466
@if($threads->hasPages())
{{ $threads->links() }}
@endif
Simple one!
Upvotes: 34
Reputation: 125
@if($products->currentPage() > 1)
{{$products->links()}}
@endif
Upvotes: -1
Reputation: 129
Juse use below format
@if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )
{{$products->links()}}
@endif
Upvotes: -1
Reputation: 1117
Don't use a check on the base class of the variable. This could lead to problems with changing base classes in future Laravel versions. Simply check whether the method links exists:
@if(method_exists($products, 'links'))
{{ $products->links() }}
@endif
Upvotes: 6
Reputation: 937
As of laravel 7 you can now do this:
@if( $vehicles->hasPages() )
{{ $vehicles->links() }}
@endif
Upvotes: 8
Reputation: 451
laravel paginate have 2 type :
Based on the above conditions, you can try this solution :
@if(
$products instanceof \Illuminate\Pagination\Paginator ||
$products instanceof Illuminate\Pagination\LengthAwarePaginator
)
{{ $products->links() }}
@endif
Upvotes: 1
Reputation:
paginate(0)
instead of get()
{{$products->links}}
. no @if @endif
needed.Upvotes: 0
Reputation: 7
Corrected code (add "isset"):
@if(isset($products->links()))
{{$products->links()}}
@endif
Shorter version:
{{$products->links() ?? ''}}
It will work for paginate, simplePaginate and when there is no pagination. The solution with "$products->hasMorePages()" will not display pagination links on the last page.
Upvotes: -1
Reputation: 332
Another way:
@if (class_basename($products) !== 'Collection')
{{ $products->links() }}
@endif
You can use PHP function: get_class($products) - to get full class name. Laravel should have some function to check ->paginate() is in use.
Upvotes: 0
Reputation: 767
The beautiful way:
@if ($products->hasMorePages())
{{ $products->links() }}
@endif
Click here to see the official documentation
Upvotes: 8
Reputation: 9988
Try like this
@if($products instanceof \Illuminate\Pagination\AbstractPaginator)
{{$products->links()}}
@endif
You need to check wheater the $products
is instance of Illuminate\Pagination\AbstractPaginator
. It can be an array or Laravel's Collection
as well.
Upvotes: 11
Reputation: 1439
This works perfectly. Check if $products
is an instance of Illuminate\Pagination\LengthAwarePaginator
then display the pagination links.
@if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )
{{$products->links()}}
@endif
Upvotes: 45