我正在试图弄清楚如何从数组更新和Eloquent ORM模态。而不是逐场进行。这是我到目前为止所做的。
public static function updatePatient($id){
$patient_payload = Input::all(); // eg. array('patient_first_name'=>'Test', 'patient_last_name'=>'TEST')
$patient_to_update = Patient::find($id);
/*
|--------------------------------------------------------------------------
| Validate the request
|--------------------------------------------------------------------------
*/
if(!$patient_to_update)
return Response::json(array('error'=>'No patient found for id given'), 400);
/*
|--------------------------------------------------------------------------
| Update the patient entry.
|--------------------------------------------------------------------------
*/
$patient_to_update->update($patient_payload);
$patient_to_update->save();
return Response::json(array('success'=>'Patient was updated'));
}
这会引发一个laravel模型错误,只是说:'patient_first_name',是的,patient_first_name是db上的col。作为一个解决方法,我刚刚这样做,这是有效的。
public static function updatePatient($id){
$patient_payload = Input::all();
$patient_to_update = Patient::find($id);
/*
|--------------------------------------------------------------------------
| Validate the request
|--------------------------------------------------------------------------
*/
if(!$patient_to_update)
return Response::json(array('error'=>'No patient found for id given'), 400);
/*
|--------------------------------------------------------------------------
| Update the patient entry.
|--------------------------------------------------------------------------
*/
DB::table('patients')
->where('id',$id)
->update($patient_payload);
//update laravel timestamps
$patient_to_update->touch();
return Response::json(array('success'=>'Patient was updated'));
}