Use missing() callbacks on route model bindings to return custom JSON responses or specialized error pages when records are not found.
By default, implicit route model binding throws a 404 ModelNotFoundException when a record missing from the database is requested.
You can customize this missing behavior per route by attaching the ->missing() closure:
use App\Http\Controllers\PostController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::get('/posts/{post:slug}', [PostController::class, 'show'])
->missing(function (Request $request) {
if ($request->wantsJson()) {
return response()->json(['error' => 'Post no longer exists'], 404);
}
return redirect()->route('posts.index')
->with('warning', 'Requested post was not found.');
});
- Customizes 404 fallback logic per individual route or resource
- Prevents raw 404 exception screens for friendly user redirects
- Works directly with custom route binding columns like
{post:slug}
Related Tips
View all tips →Refresh and Pessimistically Lock Models with refreshForUpdate() in Laravel 13.27
Laravel 13.27 adds refreshForUpdate(), reloading an existing model instance in-place with fresh database attributes while acquiring an exclusive FOR UPDATE row lock.
Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.