使用数组更新 Laravel Eloquent ORM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20428771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update Laravel Eloquent ORM with array
提问by krosullivan
I'm trying to figure out how to update and Eloquent ORM modal from an array. Rather than going field by field. This is what I have so far.
我试图弄清楚如何从数组中更新和 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'));
}
This throws a laravel model error just saying: 'patient_first_name' and yes, patient_first_name is a col on the db. As a work around I have just been doing this, which works.
这会引发一个 Laravel 模型错误,只是说:'patient_first_name' 是的,patient_first_name 是数据库上的一个列。作为一种解决方法,我一直在这样做,这很有效。
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'));
}
回答by Manuel Pedrera
Since what you are trying to do is mass assignment, I'd suggest to check if you have defined the $fillable
property in your Patient
model:
由于您要做的是批量分配,因此我建议检查您是否已$fillable
在Patient
模型中定义了该属性:
class Patient extends Eloquent {
protected $fillable = array('patient_payload');
}
As a security measure, Eloquent will not allow you to modify any model's attributes via mass assignment unless you specify $fillable
(white list of allowed attributes) or $guarded
(black list of forbidden attributes).
作为一项安全措施,Eloquent 不允许您通过批量分配修改任何模型的属性,除非您指定$fillable
(允许属性的白名单)或$guarded
(禁止属性的黑名单)。
Further reading:
进一步阅读: