Search code examples
phplaraveleloquenthttprequestinsert-update

Laravel patch method not supported (even on update method)


HTML PATCHING FORM

{{ 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() }}

Update method:

  public function update(FriendshipsAdminForm $request, Friendship $friendship)
    {
        $friendship->update($request->validated());
        return redirect()->route('admin.friendship.index');
    } 

Request form

class FriendshipsAdminForm extends FormRequest
{
  
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'user_id'  => 'required',
            'friend_id'  => 'required',
            'status'  => 'required',
        ];
    }
}

Route:

Route::resource('friendship', \App\Http\Controllers\Admin\FriendshipController::class);

This is my form and it's really strange. maybe I can not see a thing that is blocking me from doing this. there is not typo or syntax mistake. Is there anything about patch method I should consider even when trying update method?

UPDATE

enter image description here

ERROR MESSAGE , ERROR STATUS 405

UPDATE2 I'm using ajax for it.

I'm using ajax the way I use above with near 30 models and all are more complex than this one but all are updating and working nicely but this one is strange


Solution

  • Since you using ajax, @method is not working in that. You need to activate PATCH in your webserver, or change your ajax code to submit form in post and add _method in params. For example in jQuery

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