Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

sql - Laravel 8.x - Best way to create duplicate entries in mysql

I have a project in which I need to create 'n' number of duplicates of a record. This involves duplicating records in a child table. Right now i am INSERTing the duplicate parent record and then INSERT corresponding child records for that table.

My Parent table (portal_programs) enter image description here

My Parent table (portal_programs)

enter image description here

try {

            if ($programid) {
                $programobj = Programs::find($programid);
                if (!empty($eventobj)) {
                    
                    $duplicateprogramobj = new Programs();
                    $duplicateprogramobj->title = $programobj->title;
                    .
                    .
                    .
                   $duplicateprogramobj->save();         
                    
                    $productsobj = AppModelsProduct::select('*')
                                   ->where('program_id','=',$programid)->get();
                    
                    foreach($productsobj as $product)
                    {
                        
                        $duplicateprod = new AppModelsProduct();
                        $duplicateprod->program_id = $duplicateprogramobj->id;
                        .
                        .
                        .
                        $duplicateprod->save();        
                                
                        
                    }
                            
                   
                }
            }
        } catch (Exception $ex) {
            Log::critical($ex->getMessage());
        }

Is there any better/efficient way to achieve the same?

question from:https://stackoverflow.com/questions/65879657/laravel-8-x-best-way-to-create-duplicate-entries-in-mysql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...