Laravel 10 Import Large CSV File into Database Example
Hey,
In this quick example, let's see laravel 10 import large csv file into database. In this article, we will implement a laravel 10 large excel import csv example. step by step explain how to import large csv file into database in laravel 10. I would like to show you laravel 10 import large csv file.
Sometimes, we have large CSV files, such as 1GB, 2GB, 4GB, etc., that need to be imported into a database. Large files can cause timeouts or other issues in Laravel, but I have the perfect solution for importing large CSV files into your database. We will use Laravel's LazyCollection, DB, fopen() and fgetcsv() to read the CSV file and store the data into the database.
So, let's simply see the example code:
Step 1: Install Laravel
first of all, we need to get a fresh Laravel version application using the bellow command, So open your terminal OR command prompt and run the bellow command:
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 also we will 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 ProductSeeder class and write code of import large csv file.
Make sure you created products.csv file with "name", "amount" and "description" columns. Put that file in public folder.
Now, let's create seeder class using 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 you need to run following command for run all listed seeder:
php artisan db:seed --class=ProductSeeder
Now, you will able to see new row will be added in your products table.
Now I think you will understand how seeding works and we have to use it in our laravel app.
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Generate PDF and Send Email Example
- How to Create Zip File and Download in Laravel 10?
- Laravel 10 Auto Load More Data on Page Scroll using AJAX Example
- Laravel 10 Notification | Create Notification in Laravel 10
- Laravel 10 Custom Validation Rule Example
- Laravel 10 CKeditor Image Upload Example
- Laravel 10 Livewire Wizard Multi Step Form Tutorial
- Laravel 10 JQuery Ajax Pagination Example Tutorial
- Laravel 10 Google Autocomplete Address Example
- Laravel 10 Telescope Installation and Configuration Tutorial
- Laravel 10 Store JSON in Database Example
- Laravel 10 Many to Many Eloquent Relationship Tutorial
- Laravel 10 Resize Image Before Upload Example
- How to Get Last Executed Query in Laravel 10?