Hello, laravel web developers! In this article, we'll see how to import large CSV files in the laravel 11 database. Here, we'll import large data with chunks in laravel 11. When we import large data in a database it will take time to import.
In laravel 11, we'll use LazyCollection with chunk the data to import large CSV files. It will used to keep only a small part of the file in memory at a given time and faster execution.
By converting the collection to a LazyCollection
, we avoid having to allocate a ton of additional memory.
Laravel 11 Import Large CSV file in Database
In this step, we'll install the laravel 11 application using the following command.
composer create-project --prefer-dist laravel/laravel laravel-11-import-csv
Now, we will configure the database.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_11_import_csv
DB_USERNAME=root
DB_PASSWORD=root
Next, we'll create migration and model using the following command.
php artisan make:model Product -m
Migration:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('sku');
$table->string('price');
$table->timestamps();
});
}
Now, run the migration using the below command.
php artisan migrate
app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $guarded = [];
}
Then, we'll create a seeder file to upload a large CSV file to the MySQL database.
php artisan make:seeder ProductSeeder
Now, update the ProductSeeder.
database\seeders\ProductSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\LocalCommunity;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
class CommunitySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
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],
"sku" => $row[1],
"price" => $row[2]
];
})->toArray();
DB::table('products')->insert($records);
});
}
}
Then, run laravel 11 to import a large CSV file in the database application using the following command.
php artisan serve
Next, run the seeder using the below command;
php artisan db:seed --class=ProductSeeder
You might also like: