In this episode, we're building a neat little feature: user statuses, just like the status updates you'd see in a social media feed. We start off by creating a Status
model and tying statuses to users with a typical Laravel relationship. Once that's set up, we quickly whip up a migration, run it, and then add some example statuses for our demo users in the database.
Next up, we want to display not just our statuses, but also those from our friends, and we want them in the right order with the latest at the top. To make this work, we introduce the awesome package called Eloquent Has Many Deep, which helps us grab related models through complex, multi-level relationships—in our case, pulling statuses from all our friends. We then set up a custom method on the User
model using this package to return all statuses from the people you’re friends with.
After that, we refactor our dashboard route, swapping out inline closures for a dedicated dashboard controller, making things much more organized. Then we pass those friend statuses to the dashboard view and display them (however roughly) on the page—showing who posted what, and when.
Finally, we hit upon a classic performance gotcha: the n+1 query problem. To avoid hammering the database with unnecessary queries, we install Laravel Debug Bar, spot the issue, and fix it by eager-loading users alongside statuses. The result is a much more efficient app that won’t slow down when you or your friends start posting a lot.
All in all, this episode covers not only building out a practical feature but also making sure it performs well as things scale up. If you plan on making anything feed-like or want to get comfortable with advanced Eloquent relationships, this is a great one to watch!