How to Create Seeder with JSON data in Laravel? (ok)

https://www.itsolutionstuff.com/post/how-to-create-seeder-with-json-data-in-laravelexample.html

C:\xampp\htdocs\reset\database\migrations\2022_05_20_190911_create_countries_table.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');
  }
}

C:\xampp\htdocs\reset\app\Models\Country.php

C:\xampp\htdocs\reset\database\seeders\CountrySeeder.php

C:\xampp\htdocs\reset\database\data\country.json

How to Create Seeder with JSON data in Laravel?

By Hardik Savani August 7, 2021 Category : LaravelPlayUnmuteLoaded: 1.15%FullscreenVDO.AIHi,

In this short tutorial we will cover an laravel seed from json file example. i explained simply about how to create seeder with json file in laravel. if you have question about laravel seeder from json file then i will give simple example with solution. we will help you to give example of laravel seeder json.

Sometime we need to read long json 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 json data in laravel and you can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.

Step 1: Create Json File

in first step we will create country json file with name and code. so you can create data folder inside database folder and put that file as bellow preview:

database/data/country.json

Step 2: Create Seeder and Country Model

here, we will create migration for countries table. so let's create migration as bellow:

database/migrations/your_migtion_file.php

now let's run migration:

next, add soft delete facade in user model as like bellow:

app/Models/County.php

Read Also: How to create database seeder in Laravel 5.7?

Step 3: Create Seeder

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

Create Seeder with bellow command

database/seeders/CountrySeeder.php

now let's run seeder:

Read Also: How to Run Migration and Seeder on Laravel Vapor?

now you can see bellow added country lists on your table:

I hope it can help you.

Last updated

Was this helpful?