R
R
Ruslan2017-10-21 16:14:57
Laravel
Ruslan, 2017-10-21 16:14:57

Laravel. How to effectively store data in a model?

I have been studying laravel for a couple of months, after yii2 and of course some moments cause delight, some inconvenience.
So in Yii there are scripts and, in fact, it is very convenient for mass assignment of values, as in laravel you save data when you have a client part, where part of the data from the post request cannot be changed, and there is an admin area where all data can be changed by post request. There are a lot of fields to assign each there is no desire in different places.
I am doing this now

$model = Post::findOrFail($id);
$rules = Post::$rules;
$this->validate($request, $rules);
$data = $request->all(array_keys($rules));
$model->fill($data);
$model->update();

What solutions do you use?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zlatoslav Desyatnikov, 2017-10-21
@pxz

Firstly, you need to have other routes for the admin (other controller methods), use validation in the request: https://laravel.com/docs/5.5/validation#form-reque...
Also, to be safe, in Eloquent models have a fillable field, which is an array of field names that can be filled.
And then you won't have to write that terrible code that you gave as an example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question