laravel 使用资源路由创建控制器关联模型获取不到实例??(坑)

2022-09-11 12:49:20 浏览数 (1)

laravel 使用资源路由创建控制器关联模型获取不到实例??(坑)

我们一般使用以下命令创建资源控制器

代码语言:javascript复制
php artisan make:controller Admin/FangAttrController -r -m Models/FangAttr

然后添加资源路由

代码语言:javascript复制
Route::resource('fangattr', 'FangAttrController');

在控制器中使用edit或者update方法的时候(下面方法是自动生成的)

代码语言:javascript复制
public function edit(FangAttr $fangAttr)
{
  // 
  dd($fangAttr); 
}
public function update(Request $request, FangAttr $fangAttr)
{
  //
  dd($fangAttr);
}
public function destroy(FangAttr $fangAttr)
{
  //
  dd($fangAttr);
}

死活为空,我刚开始还以为我某个地方写的有问题呢?排查了一遍控制器,模型,视图,数据库之类的都未解决。最后我查看了一下路由列表

代码语言:javascript复制
php artisan route:list | findstr fangAttr (windows)
php artisan route:list | grep fangAttr (linux) 
php artisan route:list  (windows/linux) 

|        | POST          | admin/fangattr                 | admin.fangattr.store      | AppHttpControllersAdminFangAttrController@store      | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangattr                 | admin.fangattr.index      | AppHttpControllersAdminFangAttrController@index      | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangattr/create          | admin.fangattr.create     | AppHttpControllersAdminFangAttrController@create     | web,checkAdminLogin:login |
|        | POST          | admin/fangattr/uploadFile      | admin.fangattr.uploadFile | AppHttpControllersAdminFangAttrController@uploadFile | web,checkAdminLogin:login |
|        | DELETE        | admin/fangattr/{fangattr}      | admin.fangattr.destroy    | AppHttpControllersAdminFangAttrController@destroy    | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangattr/{fangattr}      | admin.fangattr.show       | AppHttpControllersAdminFangAttrController@show       | web,checkAdminLogin:login |
|        | PUT|PATCH     | admin/fangattr/{fangattr}      | admin.fangattr.update     | AppHttpControllersAdminFangAttrController@update     | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangattr/{fangattr}/edit | admin.fangattr.edit       | AppHttpControllersAdminFangAttrController@edit       | web,checkAdminLogin:login |

才发现,命令行自动生成的edit、update、destroy方法 的参数变量有问题,应该修改为与路由一致路由中参数为fangattr 而方法中参数为FangAttr $fangAttr,大小写有问题。需要修改路由Route::resource('fangAttr', 'FangAttrController');再次查看路由列表,完美解决

代码语言:javascript复制
|        | POST          | admin/fangAttr                 | admin.fangAttr.store      | AppHttpControllersAdminFangAttrController@store      | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangAttr                 | admin.fangAttr.index      | AppHttpControllersAdminFangAttrController@index      | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangAttr/create          | admin.fangAttr.create     | AppHttpControllersAdminFangAttrController@create     | web,checkAdminLogin:login |
|        | POST          | admin/fangAttr/uploadFile      | admin.fangattr.uploadFile | AppHttpControllersAdminFangAttrController@uploadFile | web,checkAdminLogin:login |
|        | DELETE        | admin/fangAttr/{fangAttr}      | admin.fangAttr.destroy    | AppHttpControllersAdminFangAttrController@destroy    | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangAttr/{fangAttr}      | admin.fangAttr.show       | AppHttpControllersAdminFangAttrController@show       | web,checkAdminLogin:login |
|        | PUT|PATCH     | admin/fangAttr/{fangAttr}      | admin.fangAttr.update     | AppHttpControllersAdminFangAttrController@update     | web,checkAdminLogin:login |
|        | GET|HEAD      | admin/fangAttr/{fangAttr}/edit | admin.fangAttr.edit       | AppHttpControllersAdminFangAttrController@edit       | web,checkAdminLogin:login |
  • 自己实力给自己挖坑,哭着也要填住。

0 人点赞