24

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 ?

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

13 Answers13

43

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
Richie
  • 1,398
  • 1
  • 19
  • 36
  • 7
    The best way is the method $products->hasMorePages() – Fred Vanelli May 26 '17 at 12:53
  • Keep in mind that the length aware paginator is different from the simple paginator (that does not count the total number of pages). For that, check for an instance of `\Illuminate\Pagination\Paginator`. Both extrend `\Illuminate\Pagination\AbstractPaginator` so that can be used to catch either. – Jason Oct 03 '17 at 14:00
30
@if($threads->hasPages())
  {{ $threads->links() }}
@endif

Simple one!

Mo7sin
  • 426
  • 6
  • 8
  • I had my pagination links inside a container but sometimes there were not enough results to generate pages. This solved it! – JCarlosR Sep 09 '20 at 00:30
9

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.

Filip Koblański
  • 9,718
  • 4
  • 31
  • 36
7

The beautiful way:

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

Click here to see the official documentation

Fred Vanelli
  • 662
  • 8
  • 9
7

As of laravel 7 you can now do this:

@if( $vehicles->hasPages() )
   {{ $vehicles->links() }}
@endif
Shreyansh Panchal
  • 827
  • 12
  • 22
  • This was [already posted](https://stackoverflow.com/a/48367834/1255289) as an answer 18 months prior. – miken32 Jul 18 '23 at 17:35
5

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
Dawied
  • 897
  • 10
  • 16
0

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.

0
  • just write paginate(0) instead of get()
  • Blade Template: simply use {{$products->links}}. no @if @endif needed.
0

laravel paginate have 2 type :

  • simplePaginate() will return \Illuminate\Pagination\Paginator
  • paginate() will return Illuminate\Pagination\LengthAwarePaginator

Based on the above conditions, you can try this solution :

@if(
    $products instanceof \Illuminate\Pagination\Paginator ||
    $products instanceof Illuminate\Pagination\LengthAwarePaginator
  )
 {{ $products->links() }}
@endif
Sawung Himawan
  • 441
  • 1
  • 4
  • 7
-1

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.

d48u
  • 7
  • 2
-1

Juse use below format

@if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )

   {{$products->links()}}

@endif                 
Sumon Sarker
  • 129
  • 2
  • 10
-1
@if($products->currentPage() > 1)

   {{$products->links()}}

@endif
Md Majadul Islam
  • 125
  • 1
  • 11
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – β.εηοιτ.βε Jan 31 '21 at 20:55
-1

check if products are instance of Pagination

@if($products instanceof \Illuminate\Pagination\LengthAwarePaginator)
        {{ $products->links() }}
@endif
MUHAMMAD USMAN
  • 149
  • 2
  • 6