I'm trying to make an agenda and I need to paginate through the events starting from today. But I need to be able to load the previous events as well.
Example of events
--------------------------------------------------
| id | title | starts_at | ends_at |
--------------------------------------------------
| 1 | past event | 2019-12-01 | 2019-12-02 |
--------------------------------------------------
| 2 | current event | 2020-02-01 | 2020-02-28 |
--------------------------------------------------
| 3 | future event | 2022-04-02 | 2022-04-04 |
--------------------------------------------------
I want to get the current/future N events as a date of today 2020-02-06
.
As result I want:
- The pagination result with only
current event
andfuture event
in the collection. - The pagination should have the
previousPageUrl()
set topage=1
and should only returnpast event
Right now I tried this, but it won't work since the pagination works from the start of the records. I need a pagination that works from the "middle" of the records
$perPage = 5;
$offset = Event::query()->where('ends_at', '<', today())->count();
$page = ceil($offset / $perPage);
Event::query()->orderBy('starts_at')->paginate($perPage, $columns = ['*'], $pageName = 'page', $page);