嵌套路由上的表单模型绑定-无法PATCH-在第219行的RouteCollection上返回MethodNotAllow


Form Model Binding on Nested Routes - Cannot PATCH - Returns MethodNotAllowedException on RouteCollection on Line 219

每当我试图向控制器提交PATCH请求时,我都会收到MethodNotAllowedException错误。它只发生在我的嵌套路由上,所有其他运行PATCH请求的路由都工作得很好。

routes.php:

Route::resource('customers.aircraft','AircraftController');

我的表格:

Form::model($aircraft, ['method' => 'PATCH', 'class' => 'form-horizontal', 'action' => ['AircraftController@update', $aircraft->id]])

每架飞机都属于一个客户。我的URL如下:

example.com/customers/5/aircraft/6/edit

当我收到错误消息时,我在地址栏中观察到的是:

example.com/customers/6/aircraft

我已经确定这不可能是由控制器引起的,因为我的@update方法中的第一行是dd($request);,它没有走那么远。我认为问题是路由没有获得引导我的请求所需的信息,它显然使用了aircraft_id并将其用作customer_id,但我不知道如何或为什么。

我试过这个:

Form::model($aircraft, ['method' => 'PATCH', 'class' => 'form-horizontal', 'action' => ['AircraftController@update', [$customer_id, $aircraft->id]]])

认为它需要发送customer_id,但这并不奏效。我是Laravel的新手,所以我确实认为这只是缺乏知识,但到目前为止,Stackoverflow、Laravel或Laracasts网站上没有任何帮助。

尝试使用Put()方法:

Form::model($aircraft, ['method' => 'PUT', 'class' => 'form-horizontal', 'action' => ['AircraftController@update', $aircraft->id]])

对于Route::resource,更新方法url应该是这样的:

example.com/aircraft/6
public function update(AircraftRequest $request, $aircraft_id) 
{ 
   $data = $request->all();
   dd($data['registration']); 
   $request['registration'] = strtoupper($request['registration']); 
   $aircraft->findOrFail($aircraft_id)->update($request->all());
   return redirect()->action('AircraftController@show', $aircraft_id); 
 }

所以尝试在隐藏输入中发送customer_id。让我知道它是否工作

相关文章: