Introduction to Route Model Binding in Laravel
Stop manually querying the database in your controllers. Learn how route model binding automatically injects Eloquent models into your routes.
Previously in this course, we learned how to perform basic CRUD operations using Eloquent models in performing-basic-crud-operations-in-laravel-with-eloquent. Up until now, when you needed to show a specific task, you probably wrote code that looked like this:
PHPpublic function show($id) { $task = Task::findOrFail($id); return view('tasks.show', ['task' => $task]); }
While this works, it’s repetitive. In a large application, you’ll write that findOrFail line dozens of times. Route model binding allows Laravel to handle this lookup automatically, making your controller code cleaner and more expressive.
What is Route Model Binding?
At its core, route model binding is a feature that automatically injects Eloquent model instances directly into your routes. When you define a route parameter that matches an Eloquent model’s type-hint in your controller method, Laravel handles the query for you.
If the record exists in the database, Laravel injects the model instance. If it doesn't, Laravel automatically throws a 404 Not Found response. You don't even have to write the check yourself.
Implementing Implicit Route Model Binding
To implement this, you need two things: a route parameter and a matching type-hint in your controller.
First, define your route in routes/web.php using a parameter name (e.g., task):
PHPRoute::get('/tasks/{task}', [TaskController::class, 'show']);
Next, update your show method in TaskController.php to type-hint the Task model:
PHPuse App\Models\Task; public function show(Task $task) { #6A9955">// $task is already the instance you need! return view('tasks.show', compact('task')); }
Notice how we replaced $id with Task $task. Because the route parameter is named {task} and our variable is named $task, Laravel knows to look for a Task model with that ID. It’s "implicit" because Laravel guesses the intent based on your naming conventions.
Customizing Binding Keys
Sometimes, you might want to identify a task by something other than its id—like a slug. If you have a slug column in your tasks table, you can tell Laravel to use that instead of the primary key.
In your Task model, add the getRouteKeyName method:
PHPpublic function getRouteKeyName() { return 'slug'; }
Now, if your route is /tasks/my-first-task, Laravel will automatically perform a query like Task::where('slug', 'my-first-task')->firstOrFail(). This is incredibly powerful for creating SEO-friendly URLs.
Handling 404 Errors
One of the best parts of using route model binding is that it simplifies error handling. By default, if a user visits /tasks/999 and that ID doesn't exist, Laravel throws a ModelNotFoundException.
In a standard web environment, this automatically triggers a 404 error page. You don't need to wrap your database calls in try-catch blocks or manually return abort(404). Laravel handles the "not found" state for you, ensuring your application remains secure and consistent.
Hands-on Exercise
Let's apply this to our Task Manager project:
- Open your
TaskControllerand locate theshowmethod. - Change the signature from
show($id)toshow(Task $task). - Remove the line
$task = Task::findOrFail($id);. - Visit a task URL in your browser (e.g.,
/tasks/1) and verify that the page still loads correctly. - Try visiting a non-existent task ID (e.g.,
/tasks/9999) and observe the automatic 404 page.
Common Pitfalls
- Parameter Mismatch: If your route is defined as
/tasks/{id}but your controller method expectsTask $task, binding will fail because the names don't match. Always keep your route parameter name consistent with your model variable name. - Missing Type-Hints: If you forget to add the
Tasktype-hint to the method parameter, Laravel will simply pass the string ID instead of the model instance, leading to "Call to a member function on string" errors. - Over-customization: While
getRouteKeyNameis useful, keep your primary keys asidwhenever possible. Only change the route key for specific use cases like slugs or unique identifiers.
By mastering route model binding, you're moving closer to the "Laravel way" of building applications—letting the framework handle the boilerplate so you can focus on the business logic.
Up next: We'll take this knowledge and use it to build an edit form in Updating Existing Records.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.