简体   繁体   English

Laravel 5.2使用uuid字符串作为id的隐式路由模型绑定

[英]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. 我正在安装新的laravel安装,使用uuid作为id时,隐式路由模型绑定出现了问题。

My route: 我的路线:

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

The show method of Admin\\UserController: Admin \\ UserController的show方法:

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. 因此,当我点击URL my.app/admin/users/long-uuid-string-here我希望看到用户信息,但会得到一个空的User对象。

When I add the following to the RouteServiceProvider, it works as expected: 当我将以下内容添加到RouteServiceProvider时,它可以按预期工作:

$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 该ID存在于数据库中,并且我正在使用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. 注意{users}变量。 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. 有点奇怪,但这就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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