In this episode, we're laying the groundwork for outputting posts on the page before we dive into infinite scroll.
First, we set up our database and models by creating a Post
model, migration, factory, and seeder. We give each post a body
text field, link it to a user via a foreign id, and include a simple like count that defaults to zero. Then we define the necessary relationships on the User and Post models.
Next, we use factories and seeders to generate a bunch of posts for our testing. We make sure to increment the created_at
date for each post so we’ll have a realistic timeline of posts (which will be especially important for infinite scrolling later!). We're seeding 200 posts here, each linked to a specific user in the database.
Once the data is ready, we create a Livewire component called PostIndex
to display the posts. We keep things simple for now: just grabbing all posts ordered by latest and looping through each one. For better organization and future functionality, we nest a PostItem
component for each post, passing in the post data.
We also tackle some basic styling: output the user’s avatar (with a quick helper using the UI Avatars API), show the user’s name (making sure the relationship is defined), and display the post body. Posts get separated nicely with borders and spacing, and we temporarily show the post ID next to the user name—a handy indicator as we go into infinite scrolling next.
In short: we're getting some posts on the page, complete with user data and a nice look, ready for the real fun (infinite scroll!) in the next episode.