laravel 5.6 bulk inserting json data

后端 未结 5 839
無奈伤痛
無奈伤痛 2021-01-05 14:21

I am trying to build an API to store and retrieve MCQ exam papers. I am using laravel resource class to send handle Json data. I need to insert 40 records into MySQL databas

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 14:41

    insert select query with update models array field

    $newModelsArray=ModelTable::where(....)->get();
    
    
                foreach ($newModelsArray as $objectItr) {
    
                $newObjectItr = $objectItr->replicate();
                $newObjectItr->field=newValue;
                $newObjectItr->save();
                }
    

    and there you'll update and save in to the table (clone it back to the database) ->replicate() will clone the modelObject and the ->save() will add it to the database inside the loop !

    Thanks Ali

提交回复
热议问题