Skip to content
Advertisement

How to insert data from one table to another in laravel?

I have two tables, visitors and in.

visitors contains columns, Name, Number, Purpose, and Datetime. in contains Name, Number, and InTime.

I want to fetch all the values from the visitors table into in. I have tried copying the data from visitors to in, however, if I add new rows into the visitors table, those new rows won’t be reflected in my in table.

Is there any way I can copy/insert all the data from one table to another in a way that it will also reflect the new added values in the in table?

Advertisement

Answer

You can use something like this :

$copyNotificationTemplates = MasterNotificationTemplate::query()
                                    ->each(function ($template) use ($organizationId) {
                                    $newTemplate = $template->replicate();
                                    $newTemplate->setTable('notification_templates');
                                    $newTemplate->organization_id = $organizationId;
                                    $newTemplate->save();
                                });
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement