Whilst I definitely like the idea of the ->next() method, I think this could be simplified/improved such that "consumers" can just loop over all the records without having to worry about the pagination. We've used similar approaches where we would expose service classes that returned the results and expose methods for checking/getting the next page.

Rather than wrapping the foreach in a do/while loop, what if we could just loop the all results?

As a simple example, let's create a service class to fetch Pokémon from the PokeAPI: 1 ($page - 1) * 20, 15 'limit' => 20, 16 ]); 17 18 foreach ($response['results'] as $result) { 19 yield $result; 20 }

By using the Generator returned from the Pokemon class, we can loop over all the Pokémon without needing to manually fetch further pages.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners