Step-by-Step CSV Data Seeding with Laravel 10 Tutorial

Reading Time: 6 minutes

Inside this article we will see the concept i.e Step-by-Step CSV Data Seeding with Laravel 10 Tutorial. Article contains the classified information i.e Laravel 10 Database Seeding from CSV File.

This topic is a tutorial on how to import CSV data into Laravel 10 database using data seeding. Data seeding is the process of populating a database with test or initial data. Database seeding is the process in which we feed test data to tables. We can insert data either using Faker library, manual data or means of some more data sources like CSV, JSON.

This tutorial will provide a step-by-step guide on how to set up and configure data seeding in Laravel 10 and import data from a CSV file.

Read More: Laravel 10 and Google Line Chart Integration Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve


Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.


Read More: Laravel 10 How To Integrate Line Chart Using HighChart Tutorial

Create Laravel Model & Migration

Open project into terminal and run this command to create model & migration file.

$ php artisan make:model Country -m

It will create two files –

  • Model – Country.php inside /app/Models folder
  • Migration file – 2023_03_15_123805_create_countries_table inside /database/migrations folder.

Open Migration file and write this complete code into it.


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('countries', function (Blueprint $table) {

     * Reverse the migrations.
     * @return void
    public function down()

Open model file Country.php and write this complete code into it.


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Country extends Model
    use HasFactory;

    public $timestamps = false;

Run Migration

Next, we need to migrate migration files.

$ php artisan migrate

This command will create tables inside database.

Read More: Creating a Dynamic Pie Chart with Highcharts in Laravel 10

It will create countries table in database.

Prepare CSV File (A Set of Data)

As we have table countries in which columns as id, name, sortname, phonecode.

We will take a CSV file in which the columns will be ID, Sortname, Name & Phonecode. Rest we need data into it.

When you will download the given file it will be in .txt format.

Rename file into .csv and place this countries.csv file inside /public/data folder.

Open countries.csv file, you should see –

Create Laravel Data Seeder

Next, we need to create a seeder file.

$ php artisan make:seeder CountrySeeder

It will create a file CountrySeeder.php inside /database/seeders folder.

Open CountrySeeder.php and write this complete code into it.


namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Country;

class CountrySeeder extends Seeder
     * Run the database seeds.
     * @return void
    public function run()

        $csvFile = fopen(public_path("data/countries.csv"), "r");

        $firstline = true;
        while (($data = fgetcsv($csvFile, 2000, ",")) !== FALSE) {
            if (!$firstline) {
                    "sortname" => $data['1'],
                    "name" => $data['2'],
                    "phonecode" => $data['3']
            $firstline = false;


Data Seeding

Open project into terminal and run this artisan command

$ php artisan db:seed --class=CountrySeeder

It will seed countries data into table.

We hope this article helped you to learn Step-by-Step CSV Data Seeding with Laravel 10 Tutorial in a very detailed way.

Read More: How To Use Laravel 10 to Create a Dynamic Bar Chart

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.