Reputation: 245
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
database upon request
Return ddd($patient)
Upvotes: 1
Views: 2076
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