Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

themenace's avatar

Pagination -> goto page that has today's date

Imagine a list of flights, dates in the past and into the future, so many that pagination is required. Let's say we're 6 months of past flights and 6 months of future flights (for example) I can query all flights and orderby date desc and display that on an index blade with pagination. The user would then have to use the reference links to go back some number to get to a list of flights for this week. Any ideas on how I can show the index page, already on the paginated reference page that is based on todays date ? Note: We don't know how many flights there possibly could be as these are entered periodically by the user and also we have all flights going back to inception (for reference) One thought is to query the flight table and show today(-3) orderby date ascending I suppose but there'd not be a history etc. Perhaps another page / reference /sort Thoughts ?

0 likes
4 replies
LaryAI's avatar
Level 58

One solution could be to modify the query used for pagination to include a where clause that filters the flights based on the current date. This way, the user will be taken directly to the page that contains flights for today's date.

Here's an example of how this could be done in Laravel:

// Get the current date
$currentDate = date('Y-m-d');

// Modify the query used for pagination to include a where clause that filters flights based on the current date
$flights = Flight::whereDate('date', $currentDate)
                ->orderBy('date', 'asc')
                ->paginate(10);

// Pass the $flights variable to the view
return view('flights.index', ['flights' => $flights]);

This code will filter the flights based on the current date and order them in ascending order by date. The paginate() method will then create the necessary pagination links for the filtered flights.

Note that this solution assumes that the date column in the flights table is a date type column. If it's a datetime type column, you'll need to modify the whereDate() method to use where() instead and compare only the date part of the datetime value.

1 like
mvd's avatar

@themenace

You can use the Paginator::currentPageResolver for this and you need to find how many flights there in the past.

Example: 20 flights in the past, 5 today and 20 in the future. You want to show 10 flights on a page, so the 3e page is for the flights for today.

Then you can do something like this

public function indexFlights() {

        $currentPage = 3; // calculation here

        // Run this before the query to get the flights
        Paginator::currentPageResolver(function () use ($currentPage) {
            return $currentPage;
        });

        $flights = \App\Flight::paginate(10);

        return view('flight.index', compact('flights'));
    }

Edit: And get total flights in the past

$flightsInThePast = \App\Flight::whereDate('flight_date', '<', now())->count();
1 like
Snapey's avatar

you could have two lists, each on its own page, future flights and past flights?

1 like
themenace's avatar

Thanks all for the thoughts. I ended up sort ascending where date > today - 3d and then put a html datepicker that submitted a form - it then changes the query to that date and all past flights paginated.

Please or to participate in this conversation.