Aman jain
3 min readJun 29, 2023

Laravel’s New Missing Method for Handling Missing Records.

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 utilise 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 typehint 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.

// Example 2: Modify Missing Model Behavior with Custom Exception 
// 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 users 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. Laravel will provide a 404 HTTP status code to the client when this error is thrown.
  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() 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() method.
  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 be used to 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 principle in laravel , so you can read this tutorial:

https://link.medium.com/P6eNfieZ1Ab

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