簡體   English   中英

如何在 laravel 中將數據從一個表插入到另一個表?

[英]How to insert data from one table to another in laravel?

我有兩張桌子, visitorsin

visitors包含列、 NameNumberPurposeDatetime in包含NameNumberInTime

我想將visitors表中的所有值提取到in 我嘗試將數據從visitors復制到in ,但是,如果我將新行添加到visitors表中,這些新行將不會反映在我in表中。

有什么方法可以將所有數據從一個表復制/插入到另一個表中,同時它也能反映表中in新添加值?

當您在訪問者中插入新行時,將該行也插入到“in”中,這樣您就不需要復制或其他東西。

你可以使用這樣的東西:

$copyNotificationTemplates = MasterNotificationTemplate::query()
                                    ->each(function ($template) use ($organizationId) {
                                    $newTemplate = $template->replicate();
                                    $newTemplate->setTable('notification_templates');
                                    $newTemplate->organization_id = $organizationId;
                                    $newTemplate->save();
                                });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM