我试图用表单中的post数据更新记录。每次我都会手动将表单中的值分配给模型并保存记录。
$model = CondoFacilities::find($id);
$model->facility_title = Input::get('facility_title');
$model->facility_description = Input::get('facility_description');
$model->facility_image = $imageName;
$status = $model->save();
是否有任何方法可以将表单数据分配给模型并在Laravel中保存/更新记录?与YI2:中的类似
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->save();
}
我是Laravel的新手,希望这个很棒的功能也能在Laravel中使用。
谢谢你的帮助。
为了创建模型并将其存储在数据库中,您可以直接在模型上使用create
方法,如下所示:
YourModel::create($request->all());
其中$request->all()
是表单的post请求,其中包含要创建并保存在数据库中的值。
同样,为了更新模型,您需要首先找到模型:
$model = YourModel::find($id);
其中$id
是要更新的模型的无符号整数。
$model->update($request->all());
当然,你也可以像这样内联更新方法:
YourModel::find($id)->update($request->all());
阅读更多关于Eloquent和Request的信息。