Laravel 10 Import Large CSV File into Database Example

Efficient Data Processing: Laravel provides built-in tools and libraries for handling large CSV files efficiently. It allows you to read and process the file in chunks, minimizing memory usage and improving performance. 

Step 1: Install Laravel

composer create-project laravel/laravel csv_upload_project

Step 2: Create Products Table 

php artisan make:migration create_products_table 
<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table--->id();
            $table->string('name');
            $table->string('amount');
            $table->text('description');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};
php artisan migrate

Step 3: Create Seeder

Here, we will create ProductSeeder class and write code of import large csv file. 

php artisan make:seeder ProductSeeder

 

Let's update following seeder code:

database/seeders/ProductSeeder.php

<?php 
namespace Database\Seeders;
  
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
  
class ProductSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        DB::disableQueryLog();
        DB::table('products')->truncate();
  
        LazyCollection::make(function () {
            $handle = fopen(public_path("products.csv"), 'r');
            
            while (($line = fgetcsv($handle, 4096)) !== false) {
                $dataString = implode(", ", $line);
                $row = explode(',', $dataString);
                yield $row;
            }
  
            fclose($handle);
  
        })
        ->skip(1)
        ->chunk(1000)
        ->each(function (LazyCollection $chunk) {
  
            $records = $chunk->map(function ($row) {
                          return [
                            "name" => $row[0],
                            "amount" => $row[1],
                            "description" => $row[2]
                          ];
                      })->toArray();
            
            DB::table('products')->insert($records);
  
        });
    }
}

Now you need to run following command:

php artisan db:seed --class=ProductSeeder