Inside this article we will see the concept of database seeding in laravel 8 using csv file. Laravel 8 database seeding from csv file is technique to dump test data into tables in bulk.
This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. 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.
Laravel 8 Database Seeding from JSON File Tutorial, Click here.
In this tutorial we will use CSV file to seed data into database table in Laravel 8.
Learn More –
- How to Generate Unique Slug in Laravel 8 Tutorial?
- How To Get Current Location Details in Laravel 8 Tutorial
- How to Get Files Information From Directory in Laravel 8
- How to Get Logged in User Data in Laravel 8 Tutorial
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
To start the development server of Laravel –
php artisan serve
URL: http://127.0.0.1:8000
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.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Create 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 at /app/Models folder
- Migration file – 2021_07_25_123805_create_countries_table at /database/migrations folder.
Open Migration file and write this complete code into it.
<?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->increments('id'); $table->string('sortname'); $table->string('name'); $table->string('phonecode'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('countries'); } }
Open model file Country.php and write this complete code into it.
<?php 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 create table inside database.
$ php artisan migrate
Prepare CSV File
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 Seeder
Next, we need to create a seeder file.
$ php artisan make:seeder CountrySeeder
It will create a file CountrySeeder.php at /database/seeders folder.
Open CountrySeeder.php and write this complete code into it.
<?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(public_path("data/countries.csv"), "r"); $firstline = true; while (($data = fgetcsv($csvFile, 2000, ",")) !== FALSE) { if (!$firstline) { Country::create([ "sortname" => $data['1'], "name" => $data['2'], "phonecode" => $data['3'] ]); } $firstline = false; } fclose($csvFile); } }
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 Laravel 8 Database Seeding from CSV File Tutorial in a very detailed way.
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.