In this episode, we're working on displaying comments for an article page in a Laravel app – but in a way that keeps things flexible and reusable, no matter what kind of "model" we're working with. The goal is that we can use our new comments listing on articles, episodes, or anything else that might be "commentable."
We start by cleaning up the layout a little bit, then dive into making a x-comments
Blade component. We make sure our component accepts a model (so it knows what to fetch comments for), and add the ability to pass that along from the parent page. This means our comments code can just be dropped in wherever we want to show comments, nice and DRY.
Once that's set up, we create a simple system to loop through and display each comment, making another small Blade component (x-comment-item
) for each individual comment. We wire up some dummy data, test it out with a couple of test comments, and display both the comment body and the author's name with some basic styling and spacing -- nothing too fancy yet!
Before wrapping up, we hit on some common issues, like the N+1 problem (lots of database queries per comment!) and the need for proper ordering of the comments. These problems are left as "to-dos" for the next episode.
In short, you'll see how to:
Coming up next, we'll tackle fixing the ordering and optimizing the database queries!