Laravel 不支持补丁方法(即使是更新方法)
Laravel patch method not supported (even on update method)
HTML 补丁表
{{ Form::model($model , [ 'route' => ['admin.friendship.update', $model->id] , 'class'=>'needs-validation ajax-form', 'method' => 'post' ]) }}
@method('PATCH')
{{ Form::select('user_id' , $users , null , ['id' => 'sender', 'class' => 'form-control']) }}
{{ Form::select('friend_id' , $users , null , ['id' => 'reciever', 'class' => 'form-control']) }}
{{ Form::select('status' , ['-2' => -2 , '-1' => -1 , '0' => 0 , '1' => 1] , null , ['id' => 'status', 'class' => 'form-control']) }}
{{ Form::close() }}
更新方法:
public function update(FriendshipsAdminForm $request, Friendship $friendship)
{
$friendship->update($request->validated());
return redirect()->route('admin.friendship.index');
}
申请表
class FriendshipsAdminForm extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'user_id' => 'required',
'friend_id' => 'required',
'status' => 'required',
];
}
}
路线:
Route::resource('friendship', \App\Http\Controllers\Admin\FriendshipController::class);
这是我的表格,真的很奇怪。也许我看不到阻止我这样做的事情。没有拼写错误或语法错误。 即使尝试更新方法,我是否应该考虑补丁方法?
更新
错误消息,错误状态 405
更新2
我正在使用 ajax。
我正在使用 ajax 我在近 30 个模型上使用的方式,所有模型都比这个更复杂,但都在更新并且运行良好,但这个很奇怪
由于您使用 ajax,因此 @method
无法正常工作。您需要在您的网络服务器中激活 PATCH
,或更改您的 ajax 代码以在 post 中提交表单并在参数中添加 _method
。
例如 jQuery
$.ajax({
...
method: 'POST',
data:{
...
'_method': 'PATCH',
}
});
HTML 补丁表
{{ Form::model($model , [ 'route' => ['admin.friendship.update', $model->id] , 'class'=>'needs-validation ajax-form', 'method' => 'post' ]) }}
@method('PATCH')
{{ Form::select('user_id' , $users , null , ['id' => 'sender', 'class' => 'form-control']) }}
{{ Form::select('friend_id' , $users , null , ['id' => 'reciever', 'class' => 'form-control']) }}
{{ Form::select('status' , ['-2' => -2 , '-1' => -1 , '0' => 0 , '1' => 1] , null , ['id' => 'status', 'class' => 'form-control']) }}
{{ Form::close() }}
更新方法:
public function update(FriendshipsAdminForm $request, Friendship $friendship)
{
$friendship->update($request->validated());
return redirect()->route('admin.friendship.index');
}
申请表
class FriendshipsAdminForm extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'user_id' => 'required',
'friend_id' => 'required',
'status' => 'required',
];
}
}
路线:
Route::resource('friendship', \App\Http\Controllers\Admin\FriendshipController::class);
这是我的表格,真的很奇怪。也许我看不到阻止我这样做的事情。没有拼写错误或语法错误。 即使尝试更新方法,我是否应该考虑补丁方法?
更新
错误消息,错误状态 405
更新2 我正在使用 ajax。
我正在使用 ajax 我在近 30 个模型上使用的方式,所有模型都比这个更复杂,但都在更新并且运行良好,但这个很奇怪
由于您使用 ajax,因此 @method
无法正常工作。您需要在您的网络服务器中激活 PATCH
,或更改您的 ajax 代码以在 post 中提交表单并在参数中添加 _method
。
例如 jQuery
$.ajax({
...
method: 'POST',
data:{
...
'_method': 'PATCH',
}
});