---
title: "Using missing() to Handle Missing Route Models in Laravel"
url: "https://magecomp.com/blog/handle-missing-route-models-laravel/"
date: "2026-08-14T12:59:32+00:00"
modified: "2026-08-14T12:59:35+00:00"
author:
  name: "Bharat Desai"
  url: "https://magecomp.com"
categories:
  - "Laravel"
word_count: 607
reading_time: "4 min read"
summary: "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 ..."
description: "Learn how to use Laravel 12's missing() method with Route Model Binding to handle missing models with custom JSON responses, redirects, and error handling."
keywords: "Laravel"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Use Flash Messages in Laravel 12 with Example"
    url: "https://magecomp.com/blog/use-flash-messages-laravel-12/"
  - title: "How to Log and Optimize Queries in Laravel?"
    url: "https://magecomp.com/blog/log-and-optimize-queries-laravel/"
  - title: "Laravel 11: How to Apply Limit in Routing"
    url: "https://magecomp.com/blog/laravel-11-apply-limit-in-routing/"
---

# Using missing() to Handle Missing Route Models in Laravel

_Published: August 14, 2026_  
_Author: Bharat Desai_  

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](https://magecomp.com/blog/wp-content/uploads/2024/12/Laravel-Development-Services-4-1024x284.webp)](https://magecomp.com/services/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 AppModels;
use IlluminateDatabaseEloquentModel;
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,

```
AppModelsPost::create([
   'title' => 'Laravel Route Model Binding',
   'description' => 'Learn how route model binding works in Laravel.',
]); 
AppModelsPost::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 AppModelsPost;
use IlluminateSupportFacadesRoute;
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](https://magecomp.com/blog/wp-content/uploads/2024/12/Hire-Laravel-Expert-Now-1024x284.webp)](https://magecomp.com/services/hire-laravel-developer/)


---

_View the original post at: [https://magecomp.com/blog/handle-missing-route-models-laravel/](https://magecomp.com/blog/handle-missing-route-models-laravel/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-14 12:59:36 UTC_  
