Sunday, March 5, 2017

Got 422 Unprocessable Entity when updating record using ajax and laravel 5.3 rest

I know this was asked many times in SO but non of them worked for me. I am using restful resource to do the update but seems no working instead I added a post in route.

web.php

Route::post('/projects/{project}', 'ProjectController@update');

controller:

public function update(Project $project)
    {
        $this->validate(request(),[
            'name' => 'required|min:8',
            'category' => 'required',
        ]);


        $project->name = request('name');
        $project->save();
    }

Ajax:

$.ajax({
     url : url,
     data: form.serializeArray(),
     method: 'POST',
     contentType: "application/json",
     error: function(data) {
           if (data.status === 422) {
               vm.errors = data.responseJSON
           }
     },
     success: function(data) {
            console.log(data)
     }
})



via claudios

Advertisement