Laravel Eloquent ORM replicate

前端 未结 4 1491
悲哀的现实
悲哀的现实 2020-12-18 22:19

I have a problem with replicating one of my models with all the relationships.

The database structure is as follows:

Table1: products
id
name

Table2         


        
4条回答
  •  攒了一身酷
    2020-12-18 22:52

    This worked fine on 5.5. image , media is a relation name.

    $event = Events::with('image','media')->find($event_id);
                if($event){
                    $newevent = $event->replicate();
                    $newevent->push();
                     foreach ($newevent->getRelations() as $relation => $entries)
                     {
                        foreach($entries as $entry)
                        {
                            $e = $entry->replicate();
                            if ($e->push())
                            {
                                $newevent->{$relation}()->save($e);
                            }
                        }
    
                    }                                
    
                }
    

提交回复
热议问题