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…

--

--

Aman jain

I’m a developer who shares advanced Laravel and Node.js insights on Medium.