[英]How to insert data from one table to another in laravel?
我有兩張桌子, visitors
和in
。
visitors
包含列、 Name
、 Number
、 Purpose
和Datetime
。 in
包含Name
、 Number
和InTime
。
我想將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.