我希望模型绑定id在检索给定模型时不使用表列,并且我getKeyName在Model类中进行覆盖(Service在这种情况下),但是它不起作用!
class Service extends Model{
//override
public function getRouteKey() {
return 'key';
}
}
Run Code Online (Sandbox Code Playgroud)
服务表:
id | key(string,unique) | name(string)
Run Code Online (Sandbox Code Playgroud)
我的路线文件:
Route::resource('services', 'ServiceController');
Run Code Online (Sandbox Code Playgroud)
并在ServiceController:
public function show(Service $service) {
return $service;
}
Run Code Online (Sandbox Code Playgroud)
但是当我转到mysiteurl.com/services/vps它时会显示404页。
(mysiteurl.com/services/1有效,但我不想在URL中使用id列)
如果您希望Laravel将模型绑定到具有除模型ID以外的其他值的路由,则需要getRouteKeyName()像这样重写方法:
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2619 次 |
| 最近记录: |