In this article, we will see laravel 9 import large CSV file into the database. Here, we will learn how to import a large CSV file into the database using laravel 8 and laravel 9. Many times we have a thousand records in CSV files and we want to store the records in our database.
Then we use the cron or queue jobs for it. But if we want to import the CSV file only one time then we will use the database seeder to import the records to the MySQL database.
So, let's see how to import a large CSV file in the database in laravel 9, laravel import large CSV file, upload large CSV file in laravel 8/9, and import large CSV file into MySQL.
In this step, we will install the laravel 9 application using the following command.
composer create-project --prefer-dist laravel/laravel laravel-9-import-csv
Now, we will configure database details. So, open the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_9_import_csv
DB_USERNAME=root
DB_PASSWORD=root
In this step, we will create migration and model using the following command. So, add the below code to that file.
php artisan make:model Product -m
database/migrations/create_products_table.php:
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
Changes in the app/Models/Product.php file.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $guarded = [];
}
In this step, we will create a seeder file to upload a large CSV file to the Mysql database.
php artisan make:seeder ProductSeeder
Now, update the code to import large CSV fine in laravel or PHP applications. So, add the following code to that file.
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);
});
}
}
Now, run laravel 9 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: