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

當我鏈分頁(5)時,我得到此錯誤「方法分頁不存在。」,如何分頁我的結果,請幫助,謝謝。Laravel 5.3 chain paginate過濾器

+0

您是否將paginate整合到您的視圖中? –

+0

@DiegoCespedes,{!! $ posts-> links()!!}? – Amigo

回答

0

最後我解決它通過創建我收藏的自定義分頁程序,也許這是不是最好的辦法,我無法找到一個解決方案更短,反正我的代碼現在工作正常。

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

嘗試刪除all()

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

調用未定義的方法Illuminate \ Database \ Query \ Builder :: filter() – Amigo

+0

您可能最好使用' - > where' closure代替'filter' –

+0

我需要一個類似於此的代碼,但這不起作用! '$ posts = Post :: where(Persian :: jDate('Y',strtotime('created_at'),'','Asia/Tehran','en'),$ pYear);' – Amigo