Ben Harvey Ben Harvey - 3 months ago 35
PHP Question

Laravel 5.2 implicit route model binding using uuid string as id

I'm setting up a new laravel installation and have come to an issue with implicit route model binding when using a uuid as an id.

My route:

Route:group(['prefix' => 'admin'], function(){
Route:resource('users', 'Admin\UserController');
});


The show method of Admin\UserController:

public function show(App\User $user) {
dd($user);
}


So when I hit the URL
my.app/admin/users/long-uuid-string-here
I would expect to see the user information but I get an empty User object.

When I add the following to the RouteServiceProvider, it works as expected:

$router->model('admin/users', \App\User::class);


Is there something I am missing, does implicit model binding expect an integer? Is it because it is in a route group or something else?

Yes! the id exists in the database, and I am using laravel 5.2

Answer

Since you are using resource routing, the route will be like:

Route::get('admin/users/{users}', 'Admin\UserController@show');

Note the {users} variable. It's plural. So in your show method:

change this:

public function show(App\User $user) {
    dd($user);
}

to

public function show(App\User $users) {
    dd($users);
}

It's a bit weird, but thats the problem.