【问题标题】:How to insert data from one table to another in laravel?如何在laravel中将数据从一个表插入另一个表?
【发布时间】:2021-05-14 02:03:34
【问题描述】:

我有两张桌子,visitorsin

visitors 包含列 NameNumberPurposeDatetimein 包含 NameNumberInTime

我想将visitors 表中的所有值提取到in。我尝试将数据从visitors 复制到in,但是,如果我将新行添加到visitors 表中,这些新行将不会反映在我的in 表中。

有什么方法可以将所有数据从一个表复制/插入到另一个表中,同时还能反映in 表中的新添加值?

【问题讨论】:

    标签: php sql database laravel datatable


    【解决方案1】:

    你可以使用这样的东西:

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

    【讨论】:

      【解决方案2】:

      当您在访问者中插入新行时,也要在“in”中插入这些行 你不需要复制或其他东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-15
        • 1970-01-01
        • 2013-04-25
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2015-07-02
        • 1970-01-01
        相关资源
        最近更新 更多