In this episode, we're working on a simple way to load more articles in our list—using a basic "Load More" button! We kick things off by looking at a common (but not very efficient) way to do this, which is just to bump up the number of items per page every time you click the button. This approach works, but we quickly see in the network tab that as you load more, things get slower and slower, because we're asking the database for an ever-growing number of records on every click. Not great for performance!
To improve on this, we refactor the code so instead of loading all articles every time, we only grab the next batch (e.g., 10 more) from the database when "Load More" is clicked. We do this by creating a custom articles collection. Each time the button's pressed, we increment the page counter, grab just the next page from the paginator, and push those articles onto our collection. This way, we're only making small, efficient queries without overloading the database.
We also tidy up the UI: the "Load More" button only shows up if there are actually more articles to load, thanks to Laravel's handy hasMorePages()
method.
All in all, by the end of the episode, you have a much better, though still not perfect, approach to loading more content with a button—setting you up for even faster solutions coming in the future!