In this episode, we tackle how to display all blog posts that belong to a specific tag when you click on one of the tags from our blog. But since we're working with flat files and not a relational database, there are a few extra hoops to jump through!
We start by setting up a dedicated controller and route that responds to /tags/{tag}
. Instead of relying on Eloquent relationships, we manually grab all posts and filter them to only keep those that have the chosen tag.
After getting the data, we build a new Blade view to list these filtered posts. We also enhance the page with a heading that displays which tag we're viewing, and handle the edge case—if you go to a tag that doesn't exist or isn't associated with any posts, you get a 404 error rather than a blank page.
Finally, to clean things up and avoid repeating ourselves, we extract the blog post list item into its own Blade component. This way, both the main blog index and the tag view pages use the same code to render each blog post, keeping everything DRY and tidy.
By the end, you'll have a neat tags system that works without a database, and some insight into filtering and structuring Blade components in Laravel's flat file setups.