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.
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.
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.
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.
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.
Let's apply this to our Task Manager project:
TaskController and locate the show method.show($id) to show(Task $task).$task = Task::findOrFail($id);./tasks/1) and verify that the page still loads correctly./tasks/9999) and observe the automatic 404 page./tasks/{id} but your controller method expects Task $task, binding will fail because the names don't match. Always keep your route parameter name consistent with your model variable name.Task type-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.getRouteKeyName is useful, keep your primary keys as id whenever 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.
Learn how to create a TasksController and define a route for your Task Manager, moving from simple closures to a scalable MVC structure.
Read moreMaster Laravel responses and redirects. Learn how to return views, handle HTTP redirects, and chain response methods to build a professional user experience.
Introduction to Route Model Binding
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation