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

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

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

我写这段代码只能插入 30000 行然后浏览器给我 HTTP 错误 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");
}

先不存所有数据,直接用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());
}