douwuying4709 2018-06-02 18:55
浏览 510
已采纳

控制器方法中的Laravel依赖注入失败

I have a model called Dbtable which isn't injected when used like this:

public function showEditDbTableForm(Request $request, DbTable $table) 
{

}

it only works when I do this:

public function showEditDbTableForm(Request $request, $id)
{
    $table = DbTable::find( $id );
}

Same thing happens even when I rename DbTable to DbTble

P.S.: please don't be rude with me as I'm new to Laravel framework

  • 写回答

2条回答 默认 最新

  • douchen3562 2018-06-02 19:09
    关注

    For Implicit Route Model Binding you need to make sure the parameter in the method signature has the same name as the route parameter you want to bind.

    Route::get('widgets/{widget}', 'WidgetsController@show');
    
    public function show(Widget $widget)
    

    Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

    Laravel 5.6 Docs - Routing - Implicit Model Binding

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?