Home » Php » How to check if used paginate in laravel

How to check if used paginate in laravel

Posted by: admin November 29, 2017 Leave a comment

Questions:

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 ?

Answers:

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

Questions:
Answers:

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.

Questions:
Answers:

The beautiful way:

@if ($products->hasMorePages())
    {{ $products->links() }}
@endif

Click here to see the official documentation

Questions:
Answers:

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.