【发布时间】:2013-12-24 02:12:20
【问题描述】:
我试图弄清楚如何从数组中更新和 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' 是的,patent_first_name 是数据库上的一个 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'));
}
【问题讨论】:
-
你能发布你得到的确切错误吗?