Read Json File And Create Seeder In Laravel

Hi Artisan,

Hope you are doin will. In this example tutorial i will show you an laravel seed from json file example. I will explain simply about how to create seeder with json file in laravel. if you have any question about laravel seeder from json file then i will give simple example with solution. So you will learn from this tutorial is laravel seeder json.

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

Let's the example of laravel seeder from json file:

 

Step 1: Manage Json File

In this first step i will create todos json file with userId, id, title and completed. Now you can create data folder inside database folder and put that file as bellow preview:

database/data/todo.json

[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  },
]

 

 Step 2: Create Seeder and Todo Model

Now, we will create migration for Todo model and seeder for it. so let's create migration as bellow:

php artisan make:migration create_todos_table

 

Now update migration file like below: 

database/migrations/your_migtion_file.php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateTodosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('todos', function (Blueprint $table) {
            $table->id();
            $table->string('user_id');
            $table->string('title');
            $table->boolean('completed')->default(false);
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('todos');
    }
}

 

next, update Todo model as like bellow:

app/Models/Todo.php

namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Todo extends Model
{
    use HasFactory;
  
    protected $fillable = [
        'user_id', 'title'
    ];
}

 

Step 3: Create Seeder

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

php artisan make:seeder TodoSeeder

 

database/seeders/TodoSeeder.php

namespace Database\Seeders;
  
use Illuminate\Database\Seeder;
use App\Models\Todo;
use File;
  
class TodoSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Todo::truncate();
  
        $json = File::get("database/data/todo.json");
        $todos = json_decode($json);
  
        foreach ($todos as $key => $value) {
            Todo::create([
                "user_id" => $value->user_id,
                "title" => $value->title,
                "completed" => $value->completed,
            ]);
        }
    }
}

 

We are almost done, Just run seeder:

php artisan db:seed --class=TodoSeeder

 

Read also : Laravel Force Redirect Http to Https Using htaccess

 

Hope it can help you.

 

#laravel #laravel-8x #database-seeder #json