我正试图弄清楚如何从数组中更新和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_ffirst_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'));
}
由于您要做的是批量分配,我建议检查您是否在Patient
模型中定义了$fillable
属性:
class Patient extends Eloquent {
protected $fillable = array('patient_payload');
}
作为一种安全措施,Eloquent不允许您通过批量分配修改任何模型的属性,除非您指定$fillable
(允许属性的白名单)或$guarded
(禁止属性的黑名单)。
进一步阅读:
- http://en.wikipedia.org/wiki/Mass_assignment_vulnerability