php - Laravel | Special validation of a lot of data - Stack Overflow
Sat Oct 08 2022 13:22:32 GMT+0000 (Coordinated Universal Time)
Saved by
@WMOH
use App\Project;
use Illuminate\Support\LazyCollection;
Route::get('/', function () {
LazyCollection::make(function () {
// project.csv with 300.000 rows
$filePath = storage_path('app/public/project.csv');
$handle = fopen($filePath, 'r');
while ($line = fgetcsv($handle)) {
yield $line;
}
})
->chunk(10000) //split in chunk to reduce the number of queries
->each(function ($lines) {
$list = [];
foreach ($lines as $line) {
if (isset($line[1])) {
$list[] = [
'name' => $line[1],
'email' => $line[2],
'status_id' => $line[3]
];
}
}
// insert 10000 rows in one shot
Project::insert($list);
});
/* display memory usage */
echo number_format(memory_get_peak_usage() / 1048576, 2) . ' MB';
});
content_copyCOPY
https://stackoverflow.com/questions/58014559/laravel-special-validation-of-a-lot-of-data
Comments