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'; });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter