簡體   English   中英

Laravel 5.3過濾后的鏈分頁

[英]Laravel 5.3 chain paginate after filter

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

當我鏈接paginate(5)時,出現此錯誤“方法paginate不存在。”,我如何對我的結果進行分頁,請幫忙,謝謝。

最后,我通過創建我的收藏夾的自定義分頁器解決了它,也許這不是最好的方法,我找不到更短的解決方案,無論如何我的代碼現在都可以正常工作。

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' => '']);
 

嘗試刪除all()

$posts = Post::filter(function($item) use (&$pYear){
    return Persian::jDate('Y', strtotime($item->created_at),'','Asia/Tehran','en') == $pYear;
})->sortByDesc('id')->paginate(5);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM