php - 为许多记录批量插入 laravel

标签 php mysql laravel laravel-4 eloquent

我正在尝试从 sql 表 A 中检索数据,修改一些列,然后将修改后的列插入到 sql 表 B 中。

但是我的问题是,当我使用:

    $customer= new Customer;
    $fakecustomer= new Fakecustomer;

            $fake_customer_name_records = $fakecustomer->get()->toArray();

               foreach($fake_customer_name_records as $record){

            //process columns for each record

                 $fake_customer_name_records_arry[]=array(
                  'last_name'=>$last_name,
                  'first_name'=>$first_name,
                  'home_phone'=>$phonenumber,
                  );
}

            $customer->insert($fake_customer_name_records_arry);

它只能插入大约 1000 条记录。在 Laravel 中有没有办法让我处理大约 60,000 条记录?

谢谢

最佳答案

我建议在这里使用“ block ”选项,并以“ block ”的形式处理记录。在我看来,这是更原生的方式。这是文档所说的:

Chunking Results

If you need to process a lot (thousands) of Eloquent records, using the chunk command will allow you to do without eating all of your RAM:

User::chunk(200, function($users)
{
    foreach ($users as $user)
    {
        //
    }
});

The first argument passed to the method is the number of records you wish to receive per "chunk". The Closure passed as the second argument will be called for each chunk that is pulled from the database.

阅读更多内容的链接:click

关于php - 为许多记录批量插入 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24565729/

相关文章:

php - 如果可以将 php 嵌入到 .html 文件中,为什么要使用 .php?

java - 使用语句时出现 MySQL 1064 语法错误

mysql - 错误: SQLSTATE[42S22]: Column not found: 1054

php - laravel 4 发送邮件时如何处理回调

php - 我如何从 Laravel 获取 Controller 中的查询结果 ID?

php表单问题如果图像输入为空则不会插入数据

php - 过期与清除 cookie

php - 查找最近和即将到来的日期

mysql - 如何将数据库的一列拆分为多列并将日期时间数据类型的值与字符串数据类型的值进行比较

php - 测试 Laravel Controller 时 View 中的 undefined variable