I think you have to code it yourself.
https://laravel.com/docs/5.7/queries#ordering-grouping-limit-and-offset : scroll down to skip / take.
Hello, dear Laracast community.
Never encountered such problem, but in need to implement this for sake of project.
I have a model Article, as an example.
And on the first page I'd like to present grid 3x4 of cards, but with "load more" card as last. So 11 Article cards and load more card. But for next page I need all 12 article cards.
For now, I make ->paginate(12) and simply hide last one (@if($loop->last) @continue @endif). But hiding is not a valid solution, as I'd like to have all articles to be presented.
If I missed something or not clear, let me know. Thanks in advance for all replies.
@Vilfago, thanks for a hint. Here is what I ended up with as I required Paginator object and its functionality.
Basically, I make the required query outside and put in this helper function, that generates LengthAwarePaginator for me.
/**
* Custom paginator for "load more" models excluding one element on first page.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $per_page
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
function getLoadMoreLengthAwarePaginator(\Illuminate\Database\Eloquent\Builder $query, $per_page = 12) {
// query total count from DB
$count = $query->count();
// get a page number from request
$page = request()->get('page') ?? 1;
// recalculate number of items for first page
$first_page = $per_page - 1;
// calculate offset
$perPage = $page == 1 ? $first_page : $per_page;
$offset = ($page - 2) * $perPage + $first_page;
// get a collection from DB
$reviews = $query->skip($offset)->take($perPage)->get();
// return Paginator instance
return new \Illuminate\Pagination\LengthAwarePaginator(
$reviews, $count, $per_page, $page, ['path' => request()->url(), 'query' => request()->query()]
);
}
Please or to participate in this conversation.