Ben Harvey Ben Harvey - 1 year ago 144
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) {

So when I hit the URL
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 Source

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) {


public function show(App\User $users) {

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download