0

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 and future event in the collection.
  • The pagination should have the previousPageUrl() set to page=1 and should only return past 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);
Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
  • 1
    have you looked at this similar question https://stackoverflow.com/questions/31747801/laravel-5-1-specifing-current-page-for-pagination ? – Frnak Feb 06 '20 at 11:22
  • why don't you look https://laravel.com/docs/6.x/pagination – Kamlesh Paul Feb 06 '20 at 11:23
  • @FrankProvost I think I may needs a custom paginator since the first page is not necessarily 5 records but could be between 1 or 5. – Clément Baconnier Feb 06 '20 at 11:25
  • It might aswell make sense to split it up into 2 queries / requests instead of going for the overhead of creating a custom paginator. But this highly depends on your specific use-case. – Frnak Feb 06 '20 at 11:31
  • Basically, I'm listing the events and there is a with a button `past events` at the top and `load more` at the bottom. Then I prepend/append a partial with ajax containing the events from the previous/next page. – Clément Baconnier Feb 06 '20 at 11:42

0 Answers0