This episode is for members only

Sign up to access "Build A Static File Blog with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
06. Fetching individual blog posts

Transcript

00:00
Because we're not working with Eloquent, root model binding to be able to click through and use the slug to fetch out each of the posts is going to be a little bit more tricky.
00:09
We're going to have to implement some custom root model binding for this to work. So let's just get started with the way that we would usually build this out. So I'm going to go ahead and create our new controller, try and use root model binding, pass this slug through and just see what happens. So let's go and create out a new controller in here, and this is going to be post show controller.
00:32
So specifically for showing each of our posts and let's create out a root for this. This would normally be something like just post and we would fetch that by the slug. So let's just do that and just see what happens. So I'm going to switch this over to post show controller and of course, change the name over and let's head over to our index and just start to link through to this using this anchor just to see what happens.
00:55
So let's go ahead and say root and posts show and let's pass through just the posts. That's what we would normally do with root model binding and just building up our root. So if we head over to our post show controller, we are kind of expecting to receive into here a post. So let's go ahead and try that and let's die dump on that post and see what happens.
01:20
So remember, post is not an eloquent model, it extends sheet. So we're going to see what happens. Okay, so we've got a missing required parameter for root post show. We are missing the post.
01:31
So what is going on here? Well, the first thing that we need to do is just pass in the slug. That is what we are looking for. So that's already fixed the problem.
01:39
It's not going to work like we normally would expect within Laravel because we're working, first of all, with just flat files and second of all, with a separate package. So you can see the URL is being built up properly. It is passing the slug in. It is giving us a post back, but it's doing what we would expect.
01:58
It's giving us just an empty post. It's not actually fetching this blog post from them collection of files by its slug. So to solve this, what we're going to need to do is create some custom root model binding for this. So when we do use post in root model binding, we specifically look it up from our collection of files.
02:22
So we don't actually need the slug in here because we're always going to assume that the post is being passed in as a slug. And let's head over to our root service provider under app and providers and go ahead and define out some custom root model binding for this. You don't have to do this in root service provider. You could do it anywhere else, but it just makes sense.
02:44
Okay, so to do this, we're going to go ahead and use the root facade. We're going to go and use the bind method, and we're going to say that we want to accept in a post. So that matches up to what we've defined here. Now, with the callback here, we can basically just resolve this post in any way that we need.
03:03
So we know that we are getting a slug into here. Let's just die dump on that slug just to see what happens when we hit this page. And yeah, sure enough, we've got that string. What that means is now we can go ahead and use that sheets package, fetch this specific item out by its slug, and then go ahead and return that.
03:22
And that will be available within our post show controller just here. So how do we do this? Well, let's go ahead and return, and we'll use our app here to make out a sheets instance. So let's pull this directly from the sheets package and not use the facade.
03:40
So that does exactly the same thing as the facade, but it's just more appropriate inside of a root, inside of a service provider. We're still going to go ahead and define out the collection that we need. So that is going to be the posts collection. And then we're going to say all.
03:54
So we're actually grabbing all posts here because really we can't query our file system. We have to grab them all back before we then go ahead and perform some sort of where on this. Now we are going to do a where, but remember, this is not the same as a eloquent builder. It is looking within this collection of all of our posts.
04:16
So I guess the downside to this is that we have to look in our entire collection to find one item. But again, the speed of this is going to be incredibly fast because it's all driven from our file system. So we're going to say where the slug equals the slug that we pass in. That's pretty much it.
04:35
So from that, we then go ahead and use first. So remember, these are all collection methods that we're using. So we still grab the first one. Okay, let's just die dump on this just to see if this is working.
04:47
And yeah, sure enough it is. We get all of the details for that specific blog post. We can go back and click another blog post. And yeah, sure enough, we get that specific post, which is great.
04:58
So now we can return this like so. And we get that post now in here dumped out from our controller and the same thing for this one as well. So successfully gone ahead and to find out how we want a post to be resolved with root model binding. There's one problem, though, that we're going to try and catch now.
05:18
If I add something onto the end of this slug, we know this doesn't exist. This is going to give us the same problem as we saw before. We're just going to get an empty post model, which is not really what we want, because then when we come to render the page, we're still going to have a post model,
05:34
but none of these attributes are going to be available. So something is just going to error out. What we want to do is catch this at the root model binding point and go ahead and abort this with a 404 if this returns to us null or it can't be found. So let's go over and give that a refresh.
05:51
And there we go. That is what we would expect. If the slug doesn't exist as a blog post, we just run a 404. Otherwise, we return that post.
13 episodes1 hr 11 mins

Overview

Let's build a ridiculously fast blog from scratch in Laravel, entirely driven by markdown files. No database required!

Your blog will feature everything you're used to, like syntax highlighting, post tagging and pagination. Plus, it'll render in record time.

Once you're done with the course, you'll be able to add on additional features with ease, style it up how you want and quickly create new posts since there's no need for an admin panel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.