Using missing() to Handle Missing Route Models in Laravel

With Route Model Binding in Laravel, you will get your model automatically by using the parameter existing in the route. In the case the model is not present, Laravel will generally return the 404 Not Found message.

Laravel Development Services

Now, Laravel provides a way out through the method called missing(), where you can define the actions to take in case the route model is missing.

Prerequisites

  • Composer (Latest Version) 
  • Laravel 12

Steps to Handle Missing Route Models in Laravel

  1. Create a new Laravel project
  2. Create a model and migration
  3. Add sample data
  4. Use the missing() method
  5. Test the output

Step 1: Create a New Laravel Project

First of all, create a new Laravel project utilizing Composer.

composer create-project laravel/laravel missing-route-demo

Then navigate inside the folder and setup the .env file.

cd missing-route-demo

Step 2: Create a Model and Migration

In this step, create a Post model along with its migration.

php artisan make:model Post -m

Open up the migration and modify it as follows.

Schema::create('posts', function (Blueprint $table) {
$table→id();
$table→string('title');
$table→text('description')→nullable();
$table→timestamps();
});

Then execute the command as specified below.

php artisan migrate

Step 3: Add Sample Data

Now, open the Post model and add the required fillable fields:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    protected $fillable = [
        'title',
        'description',
    ];
}

Also, you can add some sample posts using Laravel Tinker, so use the command,

php artisan tinker

Then,

\App\Models\Post::create([
   'title' => 'Laravel Route Model Binding',
   'description' => 'Learn how route model binding works in Laravel.',
]); 
\App\Models\Post::create([
   'title' => 'Laravel Eloquent Tips',
   'description' => 'Useful Eloquent techniques for Laravel applications.',
]);

Step 4: Use the missing() Method

Open the routes/web.php file.

use App\Models\Post;
use Illuminate\Support\Facades\Route;
Route::get('/posts/{post}', function (Post $post) {
return response()→json($post);
})->missing(function () {
return response()→json([
'message' => 'Post not found.',
], 404);
 });

Step 5: Test the Output

php artisan serve

Now open an existing post:

http://127.0.0.1:8000/posts/1

If post 1 exists, you will receive the post data:

{
    "id": 1,
    "title": "Laravel Route Model Binding",
    "description": "Learn how route model binding works in Laravel.",
    "created_at": "2026-03-09 11:56:12",
    "updated_at": "2026-03-17 07:08:18"
}

Now try a post ID that does not exist, so it will return output as below,

http://127.0.0.1:8000/posts/999

{
    "message": "Post not found."
}

Conclusion:

Laravel simplifies the process of fetching model instances when dealing with route parameters through route model binding. However, there are cases when one might wish to obtain greater control over the situations in which a requested model is not found.

The purpose of the method missing() is to provide the option to customize behavior through its use. One can choose to either return a JSON message, redirect a user elsewhere, etc.

Thus, having implemented route model binding together with missing() one can end up with neater and more user-friendly applications in Laravel 12 without having to endlessly perform tasks aimed at finding models and handling errors in the process.

FAQ

1. What is the missing() method in Laravel?

Missing() is a method that grants one the ability to customize the mechanism by which the route model is looked for.

2. What happens when the route model is not found in Laravel?

Under default settings, in the event the route model is not able to find the requested model, the user will receive a message saying that the requested resource is not found and providing the 404 code as confirmation.

3. Why should I use missing() in Laravel?

You can use missing() when you want to customize the response for a missing model, such as returning a JSON message, redirecting the user, or displaying a custom page.

Hire Laravel Expert Now
Previous Article

How to Create a Dynamic Event Calendar with Laravel 12 and FullCalendar?

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨