Handling Missing Posts 0:00We are now successfully pulling a post from the database. However, we've reintroduced that problem where if the user requests a post that does not exist, an error is thrown. Okay, trying to get property body if not object. How come? Well, think about it. If we run this query and there's no corresponding post, that variable will be equal to null. Then in our view, if I scroll down, you're trying to access the body property on null, and you can't do that, so we get an exception. So let's solve the problem, and I'll show you two ways. Namespace and Imports Recap 0:29and you can't do that, so we get an exception. So let's solve the problem, and I'll show you two ways. But for now, I could say, well, if we don't have a post, once again, we will abort. So if I come back and refresh, there we go. Next, a quick recap just to make sure we're all on the same page. If you're curious about that backslash, that's because we're trying to access this DB class from the global namespace root. Think about it. If we did not have that, the code would blow up, as you see here, and that's because it will assume the class is in the current namespace. Introducing Query Builder 0:55If we did not have that, the code would blow up, as you see here, and that's because it will assume the class is in the current namespace. So it's trying to find app HTTP controllers DB, and there is no class there. So we start with a backslash, or we can import it at the top. Now if I come back, that will work as well. Something to be aware of. Anyhow, let's move on. So what you see here is what we call the query builder. It's a clean, fluent API for constructing safe and secure SQL queries. And this is useful. Creating an Eloquent Model 1:22It's a clean, fluent API for constructing safe and secure SQL queries. And this is useful. However, there's an even cleaner way we could go about this while still conforming to virtually the same API, and that's called Eloquent. If I run php artisan, if I scroll up, you'll see a command to make a model, create a new Eloquent class. Let's do that now. But what are we working with here? Well, we're working with posts. So with that in mind, let's make a model called post.Well, we're working with posts. So with that in mind, let's make a model called post. Okay. Now, if I open my sidebar, you'll see it's stored within the app directory right here. Now, as you learned in the opening episode, an Eloquent model is useful in two ways. One, it provides that same API for performing SQL queries. However, it's also a place to store any relevant business logic. And we'll talk about that more in future episodes. But for now, I'm going to switch back to my controller. So the first step is to pull in post, and you'll see it has the namespace app. Querying with Eloquent 2:17But for now, I'm going to switch back to my controller. So the first step is to pull in post, and you'll see it has the namespace app. So we need to use it. Use app post. Next, we'll say post equals post where the slug, so notice it's the same thing here. Give me the first result. But I think you'll agree it's a little bit cleaner. Okay. So let's switch back to Firefox. I'll give it a refresh, and we get the exact same thing. Using firstOrFail 404 2:42So let's switch back to Firefox. I'll give it a refresh, and we get the exact same thing. And as a general rule of thumb, even though there will absolutely be cases where you reach for the query builder, most of the time, you'll instead be using Eloquent. Now, soon, we'll have a whole chapter dedicated to Eloquent. But until then, one quick thing. We can clean this section up quite a bit. First or fail. Give me the first post that matches this criteria or fail and throw what we call a model not found exception that will generate a 404.Give me the first post that matches this criteria or fail and throw what we call a model not found exception that will generate a 404. So I can get rid of that. And once again, we end up with this. Come back. Refresh. That all works. Let's request a post that does not exist, and again, we get the 404. And I'm sure you'll agree, this looks pretty good. And in fact, if you want, you can even inline this variable entirely like so.And I'm sure you'll agree, this looks pretty good. And in fact, if you want, you can even inline this variable entirely like so. It's starting to look pretty good. In fact, would you believe it, later, I'll show you how to clean this up even more by using a technique called route model binding, but one step at a time.