In this episode, we focus on prepping our database with some nicely ordered test data—super important if you're working on features like infinite scroll! The big takeaway here is that your data needs to be created in a way that makes sense, meaning each item has a realistic creation date so things appear correctly in order when you're loading more items on scroll.
We start by generating an Article
model, along with its migration, factory, and seeder. The migration gets us a simple table for articles with a title and a teaser. After running the migration, we use the factory to make fake articles with different titles and teasers.
But, if we just seed the database at this point, all our articles end up with the same or similar created_at
timestamps. That messes up ordering, which isn't helpful for our use case. So, we use Laravel's state
method and Sequence
to increment the creation date of each article, so every new one gets a timestamp one day apart from the previous. This way, when we order by created_at
, our data actually looks like a real timeline!
By the end, we have a reliable, realistic set of article records—perfect for developing and testing infinite scrolling. Next up, we'll get these onto the page and start building out the actual scroll feature.