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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners