In this episode, we're tackling the challenge of fetching individual blog posts using slugs, but we're not using Eloquent models—so things get a bit tricky!
First, we try to use Laravel's usual route model binding with the post slug and quickly discover that it doesn't just magically work the way it would with Eloquent. Because our posts are flat files managed with the Sheets package, the post isn't fetched out as we might expect—it just gives us an empty post.
To solve this, we roll up our sleeves and set up some custom route model binding inside our RouteServiceProvider
. This involves binding the slug in the route and fetching the correct post from our file collection using the Sheets package. Instead of database queries, we have to get all the posts first and then search for the one we want. While that sounds a bit inefficient, it's super fast since it's just reading from the filesystem.
Once we've got the correct post showing up when navigating by slug, we realize there's one more edge case: what if someone visits a slug that doesn't exist? Instead of returning an empty post, we update our binding to abort with a 404 so it behaves just like a real blog should.
So, by the end of this video, we've got per-post URLs working nicely—even without Eloquent—by diving into custom route binding and proper error handling!