Read CSV File And Create Database Seeder In Laravel

Hi Artisan,

In this laravel read csv file and make seeder tutorial we will cover an laravel seeder from csv file example. We will see ab example of laravel seed from csv file. You'll learn from this tutorial step by step laravel seeder csv file. Here we will explain the way of laravel seeder from csv.

Sometime we need to read csv file and store that data in your database. So here i am to give you very simple example of how to create seeder with csv data in laravel and we can use this example with laravel 6, laravel 7 and laravel 8 version.

 

Step 1: Create CSV File

In first step we will create a country csv file with the name and code. So create data folder inside database folder and put that file as bellow preview:

database/data/country.csv

name        code
Bangadesh   BD

 

Step 2: Create Country Model

In this second step, 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

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

 

No run below command to migrate

php artisan migrate

 

And update model like below

app/Models/County.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 Database Seeder

In this step, we need to create add database seeder for country lists.

php artisan make:seeder CountrySeeder

 

To create database seeder with csv file, first we need to read our csv file like below. 

database/seeders/CountrySeeder.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 almost done. Just run below command to make seeder with this csv data

php artisan db:seed --class=CountrySeeder

 

Read also : Read Json File and Create Seeder in Laravel

 

Hope it can help you.

 

#laravel #laravel-8x #database-seeder #csv