Tecnologia da Net
Tecnologia da Net

Reputation: 245

Update method of api in laravel does not work

I am implementing an Api in laravel for an android client. I am implementing CRUD. However, for some reason the update is not working correctly. It even returns the message that the user has been updated, but in the database, no update has been made. Can someone help me find the error?

Route update

Route::namespace('API')->name('api.')->group(function () {
    Route::prefix('patients')->group(function () {
        Route::put('/update/{id}', 'PatientController@update')->name('update_patients');
    });
});

Model Patient

class Patient extends Model
{

protected $fillable = [
    'name', 'email', 'cpf', 'data_nasc', 'rg', 'telefone', 'status', 'foto',
];
}

Controller Patient

public function update(Request $request, $id)
{
    try {

        $patientData = $request->all();
        $patient     = $this->patient->find($id);
        if($patient == null){
            $return = ['data' => ['msg' => 'Could not find patient']];
            return response()->json($return, 404);
        }
        $patient->update($patientData);
        $return = ['data' => ['msg' => 'Patient updated successfully!']];
        return response()->json($return, 201);
        


    } catch (Exception $e) {
        if(config('app.debug')) {
            return response()->json(ApiError::errorMessage($e->getMessage(), 1011),  500);
        }
        return response()->json(ApiError::errorMessage('There was an error performing the update operation', 1011), 500);
    }
}

Request and response

enter image description here

database upon request

enter image description here

Return ddd($patient)

enter image description here

Upvotes: 1

Views: 2076

Answers (1)

Bill Albert
Bill Albert

Reputation: 31

Turns out when you use the PUT request Laravel does not accept the form-data - as in your screenshot. Use x-www-form-urlencoded or raw instead.

Upvotes: 3

Related Questions