$posts = Post::all()->filter(function($item) use (&$pYear){
return Persian::jDate(...) == $pYear;
})->sortByDesc('id')->paginate(5);
當我鏈分頁(5)時,我得到此錯誤「方法分頁不存在。」,如何分頁我的結果,請幫助,謝謝。Laravel 5.3 chain paginate過濾器
$posts = Post::all()->filter(function($item) use (&$pYear){
return Persian::jDate(...) == $pYear;
})->sortByDesc('id')->paginate(5);
當我鏈分頁(5)時,我得到此錯誤「方法分頁不存在。」,如何分頁我的結果,請幫助,謝謝。Laravel 5.3 chain 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);
您是否將paginate整合到您的視圖中? –
@DiegoCespedes,{!! $ posts-> links()!!}? – Amigo