The classic 404 error is telling you that your requested record can't be found. APIs also need to return and present 404s, and this video, learn how to determine both the need to present a 404, as well as return one.
- [Instructor] A missing record error, or 404, is the classic file not found error page you've seen probably thousands of times surfing the web. The 404 is the response for us to inform the user that whatever they were looking for at this URL, is not to be found. In our API, the goal of the 404 is to provide for us a response when a resource isn't available with that particular ID. Now in Laravel we have two ways to solve this. We'll look at the simple way to solve it and then we'll go ahead and build out a custom solution in case you need it.
Begin by opening up your route's api.php file. Now in this file, you'll note that put and delete URLs using the poll parameter. This uses the ID passed in to identify the poll with that particular primary key. And if that poll isn't found, it returns an automatic 404. We've seen this before, but just to verify, let's try it out. We'll open up Insomnia, remembering to have started your Laravel server at this point.
We'll go to our delete method and we'll try to delete a random poll. In this case, let's try deleting 34567, which is a poll that clearly doesn't exist in our database. If we send our request, we're going to get a 404 response code and we're going to see Laravel's 404 standard response page. If you click on the Timeline button in Insomnia you'll see that first thing that's returned is a 404 Not Found.
This is that http response code I was talking about earlier. So this is the easy way for us to build in a 404 response into our API. However, note what this does. This is returning an HTML response page. We may not want that or we may want to return something differently and or we may need to perform our actions in a different way then Laravel's magical 404 works. To do this, let's open up our polls controller.
So go back to our code editor and open up App, Http, Controllers, PollsController.php. And here we're going to work with our show method, since it takes in an id. Rather than using the find method, we're going to use find or fail. So on line 17, we'll replace Poll::find with findOrFail. At this point we don't need to do anything else. This findOrFail method will cause the API endpoint to attempt to find the poll matching that id and when it doesn't Laravel will throw its standard 404 error page.
Now let's try this out. We'll go back to Insomnia. We'll copy the get poll method and this time we'll call it get poll does not exist. In this case we'll again try to get some random poll. Let's try poll 5634. We'll send the request and we'll get our 404 response page, much like we did with our delete request. Now there is a third way to solve this that we're also going to demonstrate.
This way we're not going to return Laravel's 404 page. Let's go back to the polls controller in our code editor. And in our show method above our response to json line, line 17, we'll add a new line and in here we'll call poll is equal to Poll::find and pass in our id. So this finds our poll and sets it to the poll variable. Now we can add on line 18 if is_null to check if our poll instance is null, so we'll pass in poll.
And then inside of our if statement we'll return response arrow json(null, 404). So this'll return a null json response with the http response code 404. Save this. We'll go back to Insomnia and try it one more time. So again, we're going to try accessing API poll 5634. And we get a null json response and a 404 response code.
Pretty cool. We've now build three different ways to deal with missing records in our Laravel API.
- Reviewing the basics of an API
- Basic pieces of the Laravel framework
- Reading, adding, editing, and deleting a record
- API errors and exceptions
- Transforming your API data
- Returning nested and sideloaded data
- Creating a logging middleware
- Authenticating to your API
- Basic and advanced endpoint testing