过滤后的Laravel 5.3链分页 [英] Laravel 5.3 chain paginate after filter

查看:31
本文介绍了过滤后的Laravel 5.3链分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$posts = Post::all()->filter(function($item) use (&$pYear){
    return Persian::jDate(...) == $pYear;
})->sortByDesc('id')->paginate(5);

当我链接paginate(5)时,出现此错误方法paginate不存在.",我该如何对我的结果进行分页,请帮忙,谢谢.

When I chain paginate(5), I get this error "Method paginate does not exist.", How can I paginate my result, please help, thank you.

推荐答案

最后,我通过创建我的收藏夹的自定义分页器解决了它,也许这不是最好的方法,无论如何我的代码都找不到更短的解决方案现在可以正常工作.

Finally I solved it by creating a custom paginator of my collection, Maybe this is not the best way, I couldn't find a shorter solution, anyway my code works fine now.

use Illuminate\Pagination\LengthAwarePaginator;

protected $perPage = 5;

$posts = Post::get()->filter(function($item) use (&$pYear){
    return Persian::jDate(...) == $pYear;
})->sortByDesc('id');

//this code simulates: ->paginate(5)
$posts = new LengthAwarePaginator(
         $posts->slice((LengthAwarePaginator::resolveCurrentPage() *
         $this->perPage)-$this->perPage,
         $this->perPage)->all(), count($posts),
         $this->perPage, null, ['path' => '']);
 

这篇关于过滤后的Laravel 5.3链分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆