Laravel Seeder from CSV File Example
Hi Dev,
In this short tutorial we will cover an laravel seeder from csv file. We will look at example of laravel seed from csv file. you'll learn laravel seeder csv file. Here you will learn laravel seeder from csv.
Sometime we need to read long csv file and store that data in you database and we need to do maybe in every setup then we always choose seeder for that. so here i will give you very simple example of how to create seeder with csv data in laravel and you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
Step 1: Create CSV File
in first step we will create country csv file with name and code. so you can create data folder inside database folder and put that file as bellow preview:
database/data/country.csv
Step 2: Create Seeder and Country Model
here, we will create migration for countries table. so let's create migration as bellow:
php artisan make:migration create_countries_table
database/migrations/your_migtion_file.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCountriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('code');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('countries');
}
}
now let's run migration:
php artisan migrate
next, add soft delete facade in user model as like bellow:
app/Models/County.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
use HasFactory;
protected $fillable = [
'name', 'code'
];
}
Step 3: Create Seeder
In this step, we need to create add seeder for country lists.
Create Seeder with bellow command
php artisan make:seeder CountrySeeder
database/seeders/CountrySeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Country;
class CountrySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Country::truncate();
$csvFile = fopen(base_path("database/data/country.csv"), "r");
$firstline = true;
while (($data = fgetcsv($csvFile, 2000, ",")) !== FALSE) {
if (!$firstline) {
Country::create([
"name" => $data['0'],
"code" => $data['1']
]);
}
$firstline = false;
}
fclose($csvFile);
}
}
now let's run seeder:
php artisan db:seed --class=CountrySeeder
now you can see bellow added country lists on your table:
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
- How to Create Seeder with JSON data in Laravel?
- How to Run Migration and Seeder on Laravel Vapor?
- How to Update Enum Value in Laravel Migration?
- Laravel Migration Enum Default Value Example
- Laravel Migration Add Comment to Column Example
- Laravel Import Large SQL File using Seeder Example
- Laravel 8 Livewire CRUD with Jetstream & Tailwind CSS
- Laravel 8 Database Seeder Tutorial Example
- Laravel 8 Create Custom Helper Functions Tutorial
- Laravel 7 Database Seeder Example
- Laravel 8 PayPal Integration Tutorial
- How to create database seeder in Laravel 5.7?