Playing
03. firstOrFail on the DB query builder

Transcript

00:00
You might already know or have used the first or fail method on Eloquence query builder. This has now been added to the database query builder as well.
00:09
Let's take a look. Okay. So I've gone ahead and created a user out in the database just to test this. Let's go ahead and fetch that user out.
00:16
So we'll go ahead and use the user model. Let's go and just say where the email address is Alex at codecourse.com. And then let's go ahead and use burst here just to demonstrate the difference between first or first or fail.
00:32
So I'm going to go ahead and dump that user out. And sure enough, we get that user dumped out. Now, if this user doesn't exist for any reason, so let's just pick a completely different email address.
00:41
We just get another value. Now you've probably already seen the first or fail method. If you haven't, what this is going to do is throw an exception, which will then cause a 404 status code to be returned to the browser.
00:55
Give it a refresh. And there we go. So this is really useful for when you're working inside of a controller, you're picking up some data, but you don't want to wrap this in an if
01:04
statement or use a helper, like abort if or abort unless. So let's go ahead and bring this back to the successful picking out of a user. That's all nice and working. This method has now been added to the database builder.
01:19
So if you're not using Eloquent for any particular circumstance or any reason, this is now going to work for you using the DB facade. Let's go ahead and check it out. So let's use the DB facade and we're going to go ahead and pick the table
01:33
that we want to, because this of course, isn't associated with a model. And we're going to pick this from the user's table. Let's start to build up the query, much like we just have here. So we're going to say where email, and it's purposely pick
01:45
one that doesn't exist in here. And typically what we would have to do before this change is use something like first. Go ahead and dump this out.
01:53
So let's assign this. And as you would expect, we get a null value. So we would have to do something like abort unless user and then 404. So that would be what we would normally do with that or wrap this in an if statement.
02:10
What we can now do though, is switch this over to first or fail. And that will do exactly the same thing. We'll still get a 404. If you're diving any deeper into Laravel and creating any kind of custom
02:23
catching of these exceptions, it's important to know that the two methods do not return or throw the same exception. Let's take a look at both of these. So if we go ahead and open up the query builder, which is for eloquent, you can
02:35
see that this throws a model not found exception, of course, that makes sense because eloquent deals with models and that exception makes sense, but of course for the database facade, we can't throw a model not found exception because this does not work with models.
02:51
So if we go ahead and open up the definition for this, you can see that this now throws a new record not found exception. So on the surface of it, when you're building applications, it doesn't really matter which exception it throws.
03:03
They both do exactly the same thing. They're caught and they render a 404. But if you are wrapping this in a try catch for any reason, then you'll need to make sure that if you are using the database query builder, then this does
03:16
throw a record not found exception rather than a model not found exception. So there we go. Pretty basic, but now we have a nicely mapped up API between these two things that now both use first or fail.

Episode summary

In this episode, we take a look at the firstOrFail method, which you might already know from Eloquent. The cool thing is, it's now available on the database query builder too!

We start by demoing how firstOrFail works on an Eloquent model, using a user record as an example. We experiment with both existing and non-existing users to see how the method either returns the record or throws an exception that leads to a 404 response. This is super handy when you want to fetch something in a controller without wrapping things in endless if statements or using helpers like abortIf.

Next, we switch over to the regular DB query builder with the DB facade—perfect for times when you're not using Eloquent models. Previously, you had to manually check if the result from first() was null and handle the 404 yourself. But now, you can just use firstOrFail and it will throw a 404 for you automatically.

One important note: the exceptions thrown are different. Eloquent's version throws a ModelNotFoundException, while the DB query builder throws a generic RecordNotFoundException. They both cause a 404 error, but if you're catching exceptions manually, you'll need to be aware of which one you're dealing with.

In short, firstOrFail now gives you a cleaner and more consistent way to handle not-found records whether you're using Eloquent or the plain query builder.

Episode discussion

No comments, yet. Be the first!