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
How to check if used paginate in laravel
提问by S.M_Emamian
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 ?
我有一个自定义视图,在某些功能中,我使用了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 $products
is an instance of Illuminate\Pagination\LengthAwarePaginator
then display the pagination links.
这完美地工作。检查是否$products
是Illuminate\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 $products
is instance of Illuminate\Pagination\AbstractPaginator
. It can be an array or Laravel's Collection
as well.
您需要检查 wheater 的$products
is 实例Illuminate\Pagination\AbstractPaginator
。它也可以是数组或 Laravel 的Collection
。
回答by Fred Vanelli
The beautiful way:
美丽的方式:
@if ($products->hasMorePages())
{{ $products->links() }}
@endif
回答by Shreyansh Panchal
回答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 ofget()
- Blade Template: simply use
{{$products->links}}
. no@if @endif
needed.
- 只是写
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() 是否在使用中。