Laravel Tutorials

New Laravel Route “Missing” Method

Published
New Laravel Route “Missing” Method image

Release updates

Never miss a Laravel release

Get an email whenever a new Laravel release lands.

With the release of Laravel v8.26.0, the Router has a new missing() method for a convenient way to handle missing records when using route model binding.

A new Route::()…->missing() method ships in today's Laravel release. Contributed by @hotmeteor … check out the docs here! https://t.co/gdhOAeJ3nS

— Taylor Otwell (@taylorotwell) February 2, 2021

By default, route model binding will return a 404 if someone tries to access an non-existent record. Currently, you’d need some customization to check and handle it accordingly. With the addition of the missing() method this scenario is much simpler:

Route::get('/locations/{location:slug}', [LocationsController::class, 'show'])
->name('locations.view')
->missing(function (Request $request) {
return Redirect::route('locations.index');
});

The missing() method works with route caching and should clean up scenarios where you’d like some custom handling when route model binding throws a ModelNotFound exception.

It’s features like this that make Laravel such a pleasure to use. A HUGE shout out to Adam Campbell for contributing this feature!

Learn More

This method is documented with routing under Customizing Missing Model Behavior. If you’d like to learn more about the implementation of this feature, check out Pull Request #36035.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article