I'm trying to update a record with the post data from a form. Every time I'm manually assigning the values from the form to the model and saving the record.
$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();
Is there any way to assign the form data to the model and save/update the record in Laravel ? Similar to the one in YII2:
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->save();
}
I'm new to Laravel hope this awesome feature is available in Laravel as well.
Thank you for your help.
In order to create a model and store it in the database, you can use create
method directly on the model like so:
YourModel::create($request->all());
where $request->all()
is the post request of the form with the values that you wish to create and save it in the database.
Similarly, for updating the model, you need to first find the model:
$model = YourModel::find($id);
where $id
is the unsigend integer of the model that you wish to update.
$model->update($request->all());
Of course you can also inline the update method like so:
YourModel::find($id)->update($request->all());
Read more about Eloquent and Requests
Hope this helps you out. Happy Coding. Cheers.