【问题标题】:Laravel patch method not supported (even on update method)不支持 Laravel 补丁方法(即使在更新方法上)
【发布时间】:2021-07-26 17:29:14
【问题描述】:

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 个模型相比,所有模型都比这个更复杂,但都在更新并且运行良好,但这个很奇怪

【问题讨论】:

  • 你能写出错误信息吗?
  • 您遇到服务器错误还是应用程序级错误?
  • 刚刚更新
  • 这能回答你的问题吗? How to use patch request in Laravel?
  • @ponsfrilus 阅读 update2

标签: php laravel eloquent httprequest insert-update


【解决方案1】:

由于您使用 ajax,@method 无法在其中工作。您需要在您的网络服务器中激活PATCH,或者更改您的ajax 代码以在post 中提交表单并在参数中添加_method。 例如在 jQuery 中

$.ajax({
   ...
   method: 'POST',
   data:{
        ...
        '_method': 'PATCH',
   }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2012-08-03
    • 2016-12-19
    相关资源
    最近更新 更多