如何将 1000000 行从 textarea 插入到 laravel 数据库中?

发布于 2025-01-09 12:31:46 字数 1161 浏览 4 评论 0原文

如何将 1000000 行从 textarea 插入到 laravel 8 的数据库中??????

我编写这段代码,只能插入 30000 行,然后浏览器给我 HTTP ERROR 500

我在 php.ini 中将 max_execution_time 设置为 300

这是我的代码 请帮我 。谢谢

public function mobile_store(Request $request)
{
    $data = $request->validate([
        'mobile' => ['required', 'string', 'unique:mobiles,mobile'],
    ]);
    $textAr = collect(explode("\r\n", $data['mobile']));
    $ALL = $textAr->unique();
    $Filter = $ALL->filter()->all();
    $counter_unique = count($Filter);
    $counter = count($textAr);
    $insert_data = collect();
    foreach ($Filter as $line) {
        if (strlen($line) >= 10) {
            $final = '+98' . substr($line, -10);
        }
        $insert_data->push([
            'mobile' => $final,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }
    foreach ($insert_data->chunk(5000) as $chunk) {
        Mobile::insert($chunk->toArray());
    }
    return redirect()->back()->with('success', "There were $counter_unique rows in the list and $counter non-duplicate rows were entered");
}

how can insert 1000000 row from textarea into database in laravel 8 ???????

i write this code and just can insert 30000 row and then browser give me HTTP ERROR 500

i set max_execution_time to 300 in php.ini

this is my code
please help me . thanks

public function mobile_store(Request $request)
{
    $data = $request->validate([
        'mobile' => ['required', 'string', 'unique:mobiles,mobile'],
    ]);
    $textAr = collect(explode("\r\n", $data['mobile']));
    $ALL = $textAr->unique();
    $Filter = $ALL->filter()->all();
    $counter_unique = count($Filter);
    $counter = count($textAr);
    $insert_data = collect();
    foreach ($Filter as $line) {
        if (strlen($line) >= 10) {
            $final = '+98' . substr($line, -10);
        }
        $insert_data->push([
            'mobile' => $final,
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
        ]);
    }
    foreach ($insert_data->chunk(5000) as $chunk) {
        Mobile::insert($chunk->toArray());
    }
    return redirect()->back()->with('success', "There were $counter_unique rows in the list and $counter non-duplicate rows were entered");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋竹姑娘 2025-01-16 12:31:47

先不要存储所有数据,只需使用1个foreach,每5000条记录存储数据,然后重置数组并进行下一批记录。

$insert_data = collect();
$totalRecords = 0;
$batchCount = 0;
foreach ($Filter as $line) {
    if (strlen($line) >= 10) {
        $final = '+98' . substr($line, -10);
    }
    $insert_data->push([
        'mobile' => $final,
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    if ( $batchCount++ == 5000 )  {
        // Insert data
        Mobile::insert($insert_data->toArray());
        // Reset batch collection
        $insert_data = collect();
        // Reset counter of current batch
        $batchCount = 0;
    }
    // Count of all records
    $totalRecords++;
}
// Insert remaining records
if( $insert_data->count() > 0 )
    Mobile::insert($insert_data->toArray());
}

Don't store all of the data first, just use 1 foreach and every 5000 records store the data and then reset the array and do the next batch of records.

$insert_data = collect();
$totalRecords = 0;
$batchCount = 0;
foreach ($Filter as $line) {
    if (strlen($line) >= 10) {
        $final = '+98' . substr($line, -10);
    }
    $insert_data->push([
        'mobile' => $final,
        'created_at' => Carbon::now(),
        'updated_at' => Carbon::now(),
    ]);
    if ( $batchCount++ == 5000 )  {
        // Insert data
        Mobile::insert($insert_data->toArray());
        // Reset batch collection
        $insert_data = collect();
        // Reset counter of current batch
        $batchCount = 0;
    }
    // Count of all records
    $totalRecords++;
}
// Insert remaining records
if( $insert_data->count() > 0 )
    Mobile::insert($insert_data->toArray());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文