简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM