How To Import Large CSV File Into Database in Laravel 11?

Jun 15, 2024 | Laravel 11 Laravel


Hello Dev,

In this brief example, let's explore how to import a large CSV file into a Laravel 10 database. We'll walk through a step-by-step explanation of importing large CSV files into the database using Laravel 10. I'll demonstrate how to handle large CSV files, such as 1GB, 2GB, 4GB, etc., which can potentially cause timeouts or other issues in Laravel. However, we have an effective solution for this using Laravel's LazyCollection, along with DB, fopen(), and fgetcsv(), to read the CSV file and store its data into the database.

Step 1: Install Laravel 11

If you haven't already, create a new Laravel project:

composer create-project laravel/laravel example-app
Read Also: How To Generate PDF and Send Email in Laravel 11? Step 2: Create Products Table

Here, we'll generate a database migration for the "products" table and create a corresponding model for it.

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');
    }
};

Run Migration

php artisan migrate
Read Also: Laravel 11 Custom User Login and Registration Example Step 3: Create Seeder

Here, we'll establish the ProductSeeder class and write the code to import a large CSV file.

Ensure you've prepared a products.csv file containing "name," "amount," and "description" columns, and place it in the public folder.

Now, let's generate the seeder class using the following command:

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, run following command for listed seeder:

php artisan db:seed --class=ProductSeeder

Feel free to ask if you have any questions or need further clarification!



Tags :
#Laravel 11
#Laravel
ItErrorSolution.com

ItErrorSolution.com

"Hey there! I'm a full-stack developer and proud owner of ItErrorSolution.com, based right here in India. I love nothing more than sharing handy tips and tricks with my fellow developers through easy-to-follow tutorials. When it comes to coding, I'm all about PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, CodeIgniter, and Bootstrap – been hooked on them forever! I'm all about putting in the work and staying committed. Ready to join me on this journey to coding?"