S.M_Emamian
S.M_Emamian

Reputation: 17383

How to check if used paginate in laravel

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

Answers (14)

Dan Stoian
Dan Stoian

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

MUHAMMAD USMAN
MUHAMMAD USMAN

Reputation: 326

check if products are instance of Pagination

@if($products instanceof \Illuminate\Pagination\LengthAwarePaginator)
        {{ $products->links() }}
@endif

Upvotes: -1

Mo7sin
Mo7sin

Reputation: 466

@if($threads->hasPages())
  {{ $threads->links() }}
@endif

Simple one!

Upvotes: 34

Md Majadul Islam
Md Majadul Islam

Reputation: 125

@if($products->currentPage() > 1)

   {{$products->links()}}

@endif

Upvotes: -1

Sumon Sarker
Sumon Sarker

Reputation: 129

Juse use below format

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

   {{$products->links()}}

@endif                 

Upvotes: -1

Dawied
Dawied

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

Shreyansh Panchal
Shreyansh Panchal

Reputation: 937

As of laravel 7 you can now do this:

@if( $vehicles->hasPages() )
   {{ $vehicles->links() }}
@endif

Upvotes: 8

Sawung Himawan
Sawung Himawan

Reputation: 451

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

Upvotes: 1

user13131707
user13131707

Reputation:

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

Upvotes: 0

d48u
d48u

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

Laguna Web Design
Laguna Web Design

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

Fred Vanelli
Fred Vanelli

Reputation: 767

The beautiful way:

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

Click here to see the official documentation

Upvotes: 8

Filip Koblański
Filip Koblański

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

Richie
Richie

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

Related Questions