php 57 lines · 7 steps

Streaming CSV imports in Laravel

An Artisan command imports a CSV of any size using lazy iteration and batched upserts to keep memory flat.

Explained by highlit
1<?php
2 
3namespace App\Console\Commands;
4 
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\DB;
7use Illuminate\Support\LazyCollection;
8 
9class ImportProductsCsv extends Command
10{
11 protected $signature = 'products:import {file : Path to the CSV file}
12 {--chunk=500 : Rows to insert per batch}';
13 
14 protected $description = 'Import products from a CSV file into the products table';
15 
16 public function handle(): int
17 {
18 $path = $this->argument('file');
19 
20 if (! is_readable($path)) {
21 $this->error("Cannot read file: {$path}");
22 
23 return self::FAILURE;
24 }
25 
26 $rows = LazyCollection::make(function () use ($path) {
27 $handle = fopen($path, 'r');
28 $header = fgetcsv($handle);
29 
30 while (($record = fgetcsv($handle)) !== false) {
31 yield array_combine($header, $record);
32 }
33 
34 fclose($handle);
35 });
36 
37 $imported = 0;
38 $now = now();
39 
40 $rows->chunk((int) $this->option('chunk'))->each(function ($chunk) use (&$imported, $now) {
41 $payload = $chunk->map(fn ($row) => [
42 'sku' => trim($row['sku']),
43 'name' => trim($row['name']),
44 'price_cents' => (int) round(((float) $row['price']) * 100),
45 'created_at' => $now,
46 'updated_at' => $now,
47 ])->all();
48 
49 DB::table('products')->upsert($payload, ['sku'], ['name', 'price_cents', 'updated_at']);
50 $imported += count($payload);
51 });
52 
53 $this->info("Imported {$imported} products.");
54 
55 return self::SUCCESS;
56 }
57}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1LazyCollection with a generator lets you process arbitrarily large files without loading them all into memory.
  2. 2Chunking rows and upserting in batches trades round-trips for constant memory and idempotent imports.
  3. 3Artisan signatures declare arguments and options with defaults and descriptions in one string.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Streaming CSV imports in Laravel — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code