Sometimes we need to seed in Laravel with a CSV file. If you don't know import data from csv to mysql using laravel database seeder then this laravel seeder from csv file tutorial is going to help you.

In this tutorial, I will show you how to read a csv file in laravel and make database seeder. So we will know laravel seeder csv file from this tutorial. To do so, we need a csv file. To read a csv file, first we have to open it with fopen() function and then we can read it using fgetcsv() function.

read-csv-file-and-seed-database-in-laravel

Step 1: Create Seeder and Country Model

In this step, we will create a Country model and a country seeder using the below command:

php artisan make:model Country -m
php artisan make:seeder CountrySeeder

 

Now update the country migration like:

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

 

next, update the Country model like:

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

 

Read also: Laravel 9 Factory Seed Data With Nested Relationship

 

Step 2: Update Seeder Class

Now in this step, we have to update the seeder class for reading data and seed them. So update it like below:

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 the below command and test it:

php artisan db:seed --class=CountrySeeder

 

Read also: Laravel Faker Image URL Example

 

Conclusion

I have tried to discuss the clear concept of laravel seeder from csv file. Now we know how to create seeder from csv file in laravel. Hope this how to import csv file in mysql using laravel seeder tutorial will help you.