【问题标题】:Laravel query-builder for insert data from a external db to another dbLaravel 查询生成器,用于将数据从外部数据库插入另一个数据库
【发布时间】:2018-03-09 21:09:22
【问题描述】:

我尝试在 Laravel 5.4v 中使用查询生成器将数据从外部数据库表发送到内部数据库表。你能告诉我如何修改下面的代码吗?谢谢。

DB::connection('ext_db')->table('ext_customers')->chunk(1000, function ($All){
    foreach ($All as $Data){
        DB::connection('inn_db')->table('inn_customers')->insert(
            [
                column1 => $Data->columnToCopy,
                etc..
            ]);
}};

【问题讨论】:

  • 这段代码不起作用吗?
  • @Vikash 是的。它不工作

标签: database laravel laravel-query-builder


【解决方案1】:

我最近不得不做同样的事情。这是我使用的代码:

$currencies_ext = DB::connection('ext_db')->table('currencies')->get();

foreach ($currencies_ext as $currency_ext) {
    $currency = new Currency;
    // set the values here
    $currency->save();
}

如果您要插入的模型设置为使用内部数据库,则无需为其指定连接。

【讨论】:

  • 对不起@eric-antoine-scuccimarra。但为什么在 foreach 中?这样会在数据库中添加多少行?
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多