Use macro for pagination links
This commit is contained in:
parent
eb4f479b97
commit
debbfa98d6
6 changed files with 46 additions and 28 deletions
|
@ -26,27 +26,15 @@ class FrontPageController extends Controller
|
|||
$bookmarks = Bookmark::latest()->get();
|
||||
$likes = Like::latest()->get();
|
||||
|
||||
$allItems = collect($notes)
|
||||
$items = collect($notes)
|
||||
->merge($articles)
|
||||
->merge($bookmarks)
|
||||
->merge($likes)
|
||||
->sortByDesc('updated_at');
|
||||
|
||||
$lastPage = intval(floor($allItems->count() / 10)) + 1;
|
||||
|
||||
$items = $allItems->forPage($pageNumber, 10);
|
||||
|
||||
if ($items->count() === 0) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$prevLink = ($pageNumber > 1) ? '/?page=' . ($pageNumber - 1) : null;
|
||||
$nextLink = ($pageNumber < $lastPage) ? '/?page=' . ($pageNumber + 1) : null;
|
||||
->sortByDesc('updated_at')
|
||||
->paginate(10);
|
||||
|
||||
return view('front-page', [
|
||||
'items' => $items,
|
||||
'prevLink' => $prevLink,
|
||||
'nextLink' => $nextLink,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ use App\Models\Note;
|
|||
use App\Observers\NoteObserver;
|
||||
use Codebird\Codebird;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Dusk\DuskServiceProvider;
|
||||
|
@ -47,6 +49,30 @@ class AppServiceProvider extends ServiceProvider
|
|||
|
||||
return $cb;
|
||||
});
|
||||
|
||||
/**
|
||||
* 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,
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue