laravel 如何检查是否在laravel中使用了分页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39977760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:35:24  来源:igfitidea点击:

How to check if used paginate in laravel

laravellaravel-5pagination

提问by S.M_Emamian

I have a custom view and in some functions, I used paginateand other functions I don't use paginate. now how can I check if I used paginateor not ?

我有一个自定义视图,在某些功能中,我使用了paginate其他功能,而我不使用其他功能paginate。现在我如何检查我是否使用过paginate

@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 ?

当然,我知道我可以使用一个变量作为真假来检查它,但是有没有任何本机函数来检查它?

回答by Richie

This works perfectly. Check if $productsis an instance of Illuminate\Pagination\LengthAwarePaginatorthen display the pagination links.

这完美地工作。检查是否$productsIlluminate\Pagination\LengthAwarePaginator显示分页链接的实例。

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

   {{$products->links()}}

@endif

回答by Mo7sin

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

Simple one!

简单的一个!

回答by Filip Koblański

Try like this

像这样尝试

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

   {{$products->links()}}

@endif

You need to check wheater the $productsis instance of Illuminate\Pagination\AbstractPaginator. It can be an array or Laravel's Collectionas well.

您需要检查 wheater 的$productsis 实例Illuminate\Pagination\AbstractPaginator。它也可以是数组或 Laravel 的Collection

回答by Fred Vanelli

The beautiful way:

美丽的方式:

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

Click here to see the official documentation

点击这里查看官方文档

回答by Shreyansh Panchal

You can also check if linksmethod exists on the model.

您还可以检查links模型上是否存在方法。

@if( method_exists($vehicles,'links') )
   {{  $vehicles ->links() }}
@endif

Reference

参考

回答by d48u

Corrected code (add "isset"):

更正的代码(添加“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.

它适用于分页、simplePaginate 和没有分页的情况。带有“$products->hasMorePages()”的解决方案不会在最后一页显示分页链接。

回答by d48u

  • just write paginate(0)instead of get()
  • Blade Template: simply use {{$products->links}}. no @if @endifneeded.
  • 只是写paginate(0)而不是get()
  • 刀片模板:只需使用{{$products->links}}. 没有@if @endif必要的。

回答by Sawung Himawan

laravel paginate have 2 type :

Laravel 分页有 2 种类型:

  • simplePaginate() will return \Illuminate\Pagination\Paginator
  • paginate() will return Illuminate\Pagination\LengthAwarePaginator
  • simplePaginate() 将返回 \Illuminate\Pagination\Paginator
  • paginate() 将返回 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

回答by Laguna Web Design

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.

您可以使用 PHP 函数: get_class($products) - 获取完整的类名。Laravel 应该有一些功能可以检查 -> paginate() 是否在使用中。