Answer the question
In order to leave comments, you need to log in
How to fill model fields using key=>val array before save?
there is such a task - to replace the regular function
update(
[
$key1 =>$val1,
$key2 = $val2
]);
$newModel = new ModelName;
foreach ( $data as $key=>$val) {
$newModel-> $key = $val; // вместо этой строчки нужно вставить правильную конструкцию. но как?
}
$newModel->save();
Answer the question
In order to leave comments, you need to log in
The Model class has methods for filling model properties with values from an array
- fill - subject to guard / fillable restrictions
- forceFill - without these restrictions
After filling, you need to save.
https://laravel.com/api/8.x/Illuminate/Database/El...
Link to documentation for version 8, but these methods seem to have been around for a long time. Check for your version of Lara.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question