【发布时间】:2019-09-20 09:22:01
【问题描述】:
作为参考,我使用了这篇文章和 Laravel 文档:
Laravel 5.6 getRouteKeyName() not working https://laravel.com/docs/5.8/routing#explicit-binding
在我的路线中,我有一个这样的资源数组:
Route::resources([
...
'state' => 'StateController',
...
]);
在我的控制器中,我试图通过 slug 访问模型。我的状态表和模型有 name 和 slug 列。
StateController
public function show(State $state)
{
dd($state);
// return view('state.show', compact('state'));
}
public function getRouteKeyName()
{
return 'slug';
}
如果我删除 State 模型类型转换,它会为 url 打印出字符串 indiana:http://codebase.localhost.com/state/indiana 但是当我重新输入类型转换时,它会给我一个 404。它找不到模型。
我认为getRouteKeyName 应该通过传递的字符串检索模型。
什么给了?
这是我的模特fillables
'name', 'order', 'slug'
这绝对是我表中的记录。
【问题讨论】:
-
getRouteKeyName不是应该在模型中(不是上图的控制器)吗?