Laravel 12: Import Large CSV File into Database Example

Laravel 12 Import Large CSV File into Database Example

When importing large CSV files, such as 1GB, 2GB, or even 4GB, it can be a nightmare to import them into a database. Large files can cause the process to trigger numerous timeouts, or in Laravel’s case, crush the import. Thankfully, there is an easy way to import massive CSV files into a database. Take advantage of Laravel’s LazyCollection, along with DB facade, fopen(), and fgetcsv() to stream all rows and insert them into the database without you having to do much.

Steps to Import Large CSV File into Database in Laravel 12:

Step 1: Install Laravel 12

composer create-project laravel/laravel example-app

Step 2: Create Products Table

Here, we need to create a database migration for the “products” table, and we will also create a model for the “products” table.

php artisan make:migration create_products_table

Migration:

<?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 the ProductSeeder class and write code to import a large CSV file.

Make sure you created the products.csv file with “name”, “amount”, and “description” columns. Put that file in the public folder.

Now, let’s create a seeder class using the following command:

php artisan make:seeder ProductSeeder

Let’s update the 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);
  
        });
    }
}
php artisan db:seed --class=ProductSeeder

You can now verify that a new row has been inserted into your products table.

Conclusion:

It is possible to import large CSV files into Laravel 12 if you are cognizant of performance and read the contents in chunks, and import the CSV in a way that does not incur timeouts/crashes through better optimization of the inserts.

Happy Coding!

FAQ

  1. What is Laravel?

Laravel is a web application framework, providing a start point and building block for web application development with PHP, with an expressive and elegant syntax.

  1. Why Use Laravel?

Laravel has a rich set of features, including an expressive database abstraction layer (Eloquent), powerful templating engine (Blade), built-in authentication and authorization, routing system, queues, scheduled jobs, and a command-line interface (Artisan CLI). It offers a variety of tools to aid in development and is centered on the developer experience.

Previous Article

Navigating the Age of AI Content: Why Verification Matters for SEO and Your Audience

Next Article

Magento 2: How to Show Custom Attribute in Minicart

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨