What is Route Model Binding? I’m glad you asked! That is exactly what we’ll take a look at in this post. So far we’ve been experimenting with various features of the Laravel framework as we build out a simple application that allows us to keep track of games in a database. We’ve created two routes so far, and one of them accepts an id so that we can fetch a specific game from our database. With route model binding we are going to make this step an automated process. Let’s see how it works.
The Current GamesController
At this point, we are up to using a dedicated games controller, which itself makes use of a Game model to find data in the database. Pay particular attention to the show()
method below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Game;
class GamesController extends Controller
{
public function index()
{
$games = Game::all();
return view('games.index', ['games' => $games]);
}
public function show($id)
{
$game = Game::find($id);
return view('games.show', ['game' => $game]);
}
}
Note that we have to pass in an $id of the game we are looking to find. In our routes file, this route Route::get(‘games/{id}’, ‘GamesController@show’); makes use of that {id} wildcard. So if someone types in /games/3
into the browser, the {id}
is captured as the number 3. That number is accepted into the show($id) method, and the game we want is found using Game::find($id);
Moving to Route Model Binding
To simplify this game fetching process, we can use Route Model Binding. Route Model Binding uses Dependency Injection to automatically find the game we are looking for.
Change the show()
method from this:
public function show($id)
{
$game = Game::find($id);
return view('games.show', ['game' => $game]);
}
To this:
public function show(Game $id)
{
return view('games.show', ['game' => $id]);
}
We can remove this entire line of code $game = Game::find($id);, and simply place the word Game in front of $id like so: show(Game $id). This is an example of making use of something called Type Hinting. We are passing an $id, but we are hinting to the fact that we want it to be an instance of a Game model. When we go back and test things out, it looks like everything still works as before. Cool!
It’s important to note that the wildcard name in the routes file must exactly match the type hinted variable passed to the function in the controller. In our case /games/{id} corresponds to show(Game $id). If we had tried to do something like show(Game $game) instead, the route model binding would not work.
By default, Laravel is making use of a primary key to find the record when using route model binding. If you need to customize this to work instead with something like finding by a slug, this is possible by customizing the key name in the Game model.
Route Model Binding Summary
In this quick tutorial, we had an introduction to route model binding in Laravel. We saw that it is a way to make finding specific records in the database even more streamlined, allowing us to remove a manual query from the show()
method in our controller.