best way for paginate collection:
1- add this to boot function in appProvidersAppServiceProvider
/**
* Paginate a standard Laravel Collection.
*
* @param int $perPage
* @param int $total
* @param int $page
* @param string $pageName
* @return array
*/
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
2-From hereafter for all collection you can paginate like this
$events1 = AppEvent::Where('valid_to','>=',$today)->orderByRaw('valid_to','ASC')->get();
$events2 = AppEvent::Where('valid_to','<',$today)>orderByRaw('valid_to','ASC')->get();
$events = $events1->merge($events2);
$events->paginate(5)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…