How to Use Route Missing Methods in Laravel: A Step-by-Step Tutorial

Aman jainAman jain
4 min read

Missing method

Laravel 8.26.0 introduced a new method to the Router class called missing(). This method allows you to customize the behavior of route model binding when a model cannot be found.

An exception would be thrown in previous versions of Laravel if a model wasn’t found during route model binding. If you intended to approach the matter in a different manner, this might be inconvenient. For instance, you could wish to display a unique error message or reroute the user to a different page.

The missing() method takes a closure as its argument. The closure will be executed if the model record is not found. The closure can be used to perform any desired action, such as redirecting the user to a different page or returning a 404 error.

You can utilize the missing() function, a strong tool, to increase the flexibility and reliability of your Laravel apps. It can be used to tackle various kinds of situations. Here is an example of how to use the missing() method:

Route::get('users/{user}', function (User $user) {
// ...
})->missing(function () {
// The user record was not found.
return Redirect::route('users.index');
});

Let’s Explain This Example

  1. A route that will retrieve the user with the supplied ID is defined by the Route::get() method.

  2. A type hint that indicates to Laravel that the closure expects a User model as its argument is the User $user parameter.

  3. If the user doesn’t exist, the missing() method will be executed.
    The missing() method’s closure will direct the user to the users. index path.

Some Other Examples are given Below

Example 1.

// Manage user and throw a custom exception if user not found 
Route::resource('users', UserCOntroller::class)
→missing(function (Request $request) {
  throw new NotFoundHttpException('User not found.');
});

Explanation of example 1:

  1. The Route::resource() method defines a resource route for the user’s controller.

  2. The missing() method will be called if the user does not exist.

  3. The closure in the missing() method will throw a NotFoundHttpException.

  4. When a requested resource cannot be found, a Laravel exception called the NotFoundHttpException is raised. When this error is thrown, Laravel will provide the client with a 404 HTTP status code.

  5. If the user is not present in this scenario, the missing() method will raise a NotFoundHttpException.As an outcome, Laravel will send the customer a 404 HTTP status code.

Example 2.

// Display a user and return a JSON response with error message if not found 
Route::get('/users/{user}', [UserController::class, 'show'])
→name('users.show')
→missing (function (Request $request) {
  return response()→json(['error' 'User not found. '], 484);
});

Let’s explain example 2:

  1. The Route::get() the method defines a route that will get the user with the specified ID and call the UserController@show() method to display the user.

  2. The missing() method will be called if the user does not exist.
    A JSON response with the status code 484 will be returned by the closure in the missing() technique.

  3. The request is not well-formed, which is indicated by the 484 status code.

Example .3

// Manage user and return a custom error view if user not found
Route::resource('users', UserController::class)
→missing(function (Request $request) {
return view("errors.user-not-found");
});

Let’s Explain example 3:

  • The Route::resource() method defines a resource route for the user controller.

  • The missing() method will be called if the user does not exist.

  • The closure in the missing() method will render the errors.user-not-found view.

The errors.user-not-found view is a Blade view that can display an error message to the user. The view will typically be located in the resources/views/errors directory.

In this case, the missing() method will render the errors.user-not-found view if the user does not exist. This will cause the view to be displayed to the user.

Your Laravel applications can benefit greatly from increased flexibility and control thanks to the missing() function. I recommend making use of this new function if you use route model binding.

If you want to learn about solid principles in laravel , so you can read this tutorial:

SOLID Principles in Laravel…

"When everyone else says you can't, determination says,'YES YOU CAN.'" ~ Robert M. Hensel

0
Subscribe to my newsletter

Read articles from Aman jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aman jain
Aman jain

I'm a developer who shares advanced insights and expertise through technology-related articles. Passionate about creating innovative solutions and staying up-to-date with the latest tech trends. Let's connect and explore the world of technology together!