Laravel Route Model Binding
Open any Laravel project and look at a page that shows one record, such as one invoice, one client or one order. The URL has an ID in it, like /invoices/1001. The first thing the code does is take that ID and fetch the record from the database. Then it checks whether the record exists. If it does not, it returns a 404 page.
Now count how many pages in your app work like this: show, edit, update, delete, download PDF, send reminder. In an invoicing app that is a dozen methods, and each one starts with the same lookup and the same “what if it is missing” check. It is boring code, and boring code is where bugs hide.
Route model binding removes it. You tell Laravel which model a URL segment represents, and Laravel fetches the record before your code runs. If the record does not exist, Laravel sends the 404 for you.
The Manual Way: Looking Up the Record Yourself
Here is a route that shows one invoice, written the way you would without model binding. It assumes you have an Invoice model and an invoices table. You will create these in the Database and Eloquent chapters, so for now just focus on the routing side.
use App\Models\Invoice;
use Illuminate\Support\Facades\Route;
Route::get('/invoices/{invoice}', function (string $invoice) {
$invoice = Invoice::findOrFail($invoice);
return "Invoice {$invoice->invoice_number}: ₹{$invoice->amount}";
})->whereNumber('invoice');
The findOrFail() call looks up the invoice by its ID and automatically produces a 404 when nothing is found. This works, but you would repeat that line in every route and controller method that receives an invoice ID.
Implicit Route Model Binding
Now the same route with model binding. The only change is the function argument:
use App\Models\Invoice;
Route::get('/invoices/{invoice}', function (Invoice $invoice) {
return "Invoice {$invoice->invoice_number}: ₹{$invoice->amount}";
})->whereNumber('invoice');
There is no lookup and no 404 check. By the time your function runs, $invoice is already the full Invoice record from the database. This is called implicit binding, because Laravel works it out from the way you wrote the code.
How Laravel Matches the Model
Two things must be true for implicit binding to work:
- The function argument is type-hinted with an Eloquent model class, like
Invoice $invoice. - The argument name matches the placeholder name in the URL, like
{invoice}and$invoice.
When both match, Laravel takes the value from the URL, looks it up in the model’s table (by id unless you say otherwise), and injects the result. If no record is found, Laravel returns a 404 response automatically.
This differs from the plain parameters you saw in the Route Parameters lesson. Plain parameters are matched by position, and the name does not matter. Model-bound parameters are matched by name, so the name must be exact.
As you saw in the Request Lifecycle lesson, the router matches the URL first, and middleware resolves bindings right after. That is why your controller code runs only after the model is already loaded.
Using It in a Controller
Binding works the same way in controller methods, which you will build in the Controllers chapter:
// routes/web.php
Route::get('/invoices/{invoice}', [InvoiceController::class, 'show'])
->whereNumber('invoice')
->name('invoices.show');
// app/Http/Controllers/InvoiceController.php
public function show(Invoice $invoice)
{
return view('invoices.show', compact('invoice'));
}
The controller method is now two lines, and it no longer cares how invoices are looked up. This fits the MVC advice from earlier: routes point, controllers coordinate, models own the data.
Binding by Another Column
By default, Laravel looks up the record by id. But your clients do not think in database IDs. They think in invoice numbers like INV-2026-0042. A URL such as /invoices/INV-2026-0042 is friendlier and does not reveal how many invoices you have created.
Choosing the Column in the Route
Add a colon and the column name inside the placeholder:
Route::get('/invoices/{invoice:invoice_number}', function (Invoice $invoice) {
return "Invoice {$invoice->invoice_number}: ₹{$invoice->amount}";
})->name('invoices.show');
Now /invoices/INV-2026-0042 finds the invoice where invoice_number equals that value. Make sure this column is unique in your table, otherwise Laravel returns the first match.
Setting the Default Column on the Model
If you always want to find invoices by invoice_number, you can set that once on the model instead of repeating it in every route. Laravel 13 provides the RouteKey attribute for this:
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\RouteKey;
use Illuminate\Database\Eloquent\Model;
#[RouteKey('invoice_number')]
class Invoice extends Model
{
// ...
}
With this in place, a plain {invoice} placeholder uses invoice_number. Older tutorials do the same thing by overriding a method called getRouteKeyName() on the model, and you may still see that in existing projects. Both approaches work.
This also connects to the previous lesson. When you pass a model to route(), Laravel uses the model’s route key automatically:
route('invoices.show', ['invoice' => $invoice]);
// http://localhost:8000/invoices/INV-2026-0042
You do not need to write $invoice->invoice_number yourself. The URL and the lookup always use the same column.
Scoped Bindings for Nested Routes
Consider a route that shows one invoice of one client:
Route::get('/clients/{client}/invoices/{invoice}', function (Client $client, Invoice $invoice) {
return "{$client->name}: {$invoice->invoice_number}";
});
There is a problem. Suppose invoice 1001 belongs to client 7. A visitor can open /clients/9/invoices/1001, and Laravel will happily load client 9 and invoice 1001 as two unrelated records. The URL claims the invoice belongs to client 9, but it does not.
Scoped bindings fix this. Laravel then loads the invoice only from the invoices of the given client:
Route::get('/clients/{client}/invoices/{invoice}', function (Client $client, Invoice $invoice) {
return "{$client->name}: {$invoice->invoice_number}";
})->scopeBindings();
Now /clients/7/invoices/1001 works, and /clients/9/invoices/1001 returns a 404. Laravel finds the invoice through the client’s invoices relationship, so the Client model needs a relationship method with that name. You will build relationships in the Eloquent Relationships chapter.
Scoping also switches on automatically when you use a custom key on the child parameter, like {invoice:invoice_number}. You can apply it to a whole group with Route::scopeBindings()->group(...), which fits well with the groups from the previous lesson. To turn it off for one route, call withoutScopedBindings().
Customising What Happens When the Model Is Missing
The Default 404
When no record matches, Laravel returns a 404 page. For most routes this is exactly what you want, because a missing invoice is simply a page that does not exist.
Redirecting Instead with missing()
Sometimes a friendlier response helps, such as sending the user back to the list. Use missing() on the route:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
Route::get('/invoices/{invoice:invoice_number}', [InvoiceController::class, 'show'])
->name('invoices.show')
->missing(function (Request $request) {
return Redirect::route('invoices.index');
});
If the invoice number does not exist, the visitor lands on the invoices list instead of an error page.
Including Soft Deleted Records
Some apps never truly delete an invoice. They mark it as deleted and hide it, which is called soft deleting. You will learn it in the Eloquent ORM chapter. By default, binding treats such records as missing. To allow them on a specific route, for example an admin page that restores invoices, add withTrashed():
Route::get('/admin/invoices/{invoice}', function (Invoice $invoice) {
return "Invoice {$invoice->invoice_number} (deleted records allowed)";
})->withTrashed();
Enum Binding
In the Route Parameters lesson, you limited a parameter to a few allowed words with whereIn(). If you use PHP backed enums, binding gives you a cleaner way. Here is an enum for invoice statuses:
namespace App\Enums;
enum InvoiceStatus: string
{
case Pending = 'pending';
case Paid = 'paid';
case Overdue = 'overdue';
}
Type-hint it in the route, and Laravel runs the route only when the URL segment matches one of the values:
use App\Enums\InvoiceStatus;
Route::get('/invoices/status/{status}', function (InvoiceStatus $status) {
return "Showing {$status->value} invoices";
});
/invoices/status/paid works, and /invoices/status/cancelled returns a 404. You also get a proper enum object inside the function, which is safer than comparing strings.
Explicit Binding
Implicit binding covers almost everything. Explicit binding is for the cases where you want to decide yourself how a placeholder turns into a model. You register it inside the boot() method of AppServiceProvider, the same place where you defined global patterns earlier:
// app/Providers/AppServiceProvider.php
use App\Models\Invoice;
use Illuminate\Support\Facades\Route;
public function boot(): void
{
Route::model('invoice', Invoice::class);
}
For full control over the lookup, use Route::bind() and return the model yourself:
public function boot(): void
{
Route::bind('invoice', function (string $value) {
return Invoice::where('invoice_number', $value)->firstOrFail();
});
}
Now every {invoice} placeholder in the application uses this lookup. Keep this tool for special cases, such as a lookup that needs two columns. If you only need a different column, the {invoice:invoice_number} form or the RouteKey attribute is simpler.
Common Mistakes
Mistake 1: The Variable Name Does Not Match the Placeholder
Route::get('/invoices/{invoice}', function (Invoice $inv) {
return $inv->invoice_number;
});
The placeholder is {invoice} but the variable is $inv. Laravel cannot match them, so it does not bind anything. You get no error, but you receive an empty Invoice object, and fields such as invoice_number are null. Rename the variable to $invoice so the names match exactly.
Mistake 2: Binding by One Column but Searching by Another
Route::get('/invoices/{invoice}', function (Invoice $invoice) {
return $invoice->invoice_number;
});
If you now visit /invoices/INV-2026-0042, you get a 404, because Laravel searched the id column for that text. Either use {invoice:invoice_number} in the route, or set the default key on the model.
The same applies to constraints. A route with {invoice:invoice_number} and ->whereNumber('invoice') will always return a 404, because an invoice number is never purely numeric. Update the constraint to match the column you are binding.
Mistake 3: Scoped Binding Without the Relationship
If you use scoped bindings and the parent model has no matching relationship, Laravel cannot find the child through it:
BadMethodCallException: Call to undefined method App\Models\Client::invoices()
Add an invoices() relationship to the Client model. The relationship name should be the plural of the child placeholder name.
Mistake 4: Assuming Binding Checks Permissions
Route model binding only finds the record. It does not check whether the logged-in user is allowed to see it. If every user can open /invoices/1001, then any user can read any invoice just by changing the number in the address bar. This is a real and common security mistake. Scoping to the current user’s data and using policies solves it, and you will learn both in the Authorization chapter.
Quick Summary
- Route model binding turns a URL value into a loaded model before your code runs.
- Implicit binding needs a type-hinted model and an argument name that exactly matches the placeholder.
- A missing record gives a 404 automatically.
- Use
{invoice:invoice_number}to bind by another column, or set the default with theRouteKeyattribute on the model. - Passing a model to
route()uses the model’s route key automatically. scopeBindings()makes sure a child record belongs to its parent in nested routes, and needs a relationship on the parent model.missing()customises the missing-model response, andwithTrashed()allows soft deleted records.- Backed enums can be bound, and only valid values reach the route.
- Explicit binding with
Route::model()orRoute::bind()lives inAppServiceProvider::boot()and is for special cases. - Binding finds records but does not check permissions.
What’s Next
You have now seen routes with parameters, constraints, names, groups and model binding, and a typical resource such as invoices needs the same set of routes every time: list, show, create, edit, update and delete. In the next lesson, Resource Routes & route:list, you will learn how Laravel creates that whole set with a single line, and how to inspect every route in your project from the terminal.