Seeders Tutorial in Laravel 11

 <?php


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class studentstwo extends Model
{
    //
}
Above File is app\Models\studentstwo.php File





Below File is database\json\studentstwo.json File
[
    {
        "name": "Yahoo Baba",
        "age": 56,
        "city": "Ahmedabad"
    },
    {
        "name": "Hello",
        "age": 21,
        "city": "Mumbai"
    },
    {
        "name": "World",
        "age": 67,
        "city": "Ahmedabad"
    },
    {
        "name": "Amitabh Bachchan",
        "age": 33,
        "city": "Pune"
    },
    {
        "name": "Ghanshyam Naredi",
        "age": 45,
        "city": "Delhi"
    },
    {
        "name": "Manish Jakhania",
        "age": 67,
        "city": "Ahmedabad"
    }
]





Below File is database\migrations\2024_12_09_061859_create_studentstwos_table.php File
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('studentstwos', function (Blueprint $table) {
            $table->id();
            $table->string("name");
            $table->integer("age");
            $table->string("city");
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('studentstwos');
    }
};





Below File is database\seeders\StudentsTwoSeeder.php File
<?php

namespace Database\Seeders;

use App\Models\studentstwo;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;

class StudentsTwoSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // $studentstwo = collect(
        //     [
        //         [
        //             "name" => "Yahoo",
        //             "age" => 22,
        //             "city" => "Ahmedabad"
        //         ],
        //         [
        //             "name" => "Baba",
        //             "age" => 34,
        //             "city" => "Ahmedabad"
        //         ],
        //         [
        //             "name" => "Hello",
        //             "age" => 54,
        //             "city" => "Mumbai"
        //         ],
        //         [
        //             "name" => "World",
        //             "age" => 11,
        //             "city" => "Mumbai"
        //         ]
        //     ]
        // );

        // $studentstwo->each(function ($student) {
        //     studentstwo::insert($student);
        // });

        // For inserting Single Data in Database as shown below :
        // studentstwo::create([
        //     "name" => "Hello brother",
        //     "age" => 89,
        //     "city" => "Ahmedabad"
        // ]);

        // For inserting Fake data using seeder as shown below :
        // for ($i = 1; $i <= 10; $i++) {
        //     studentstwo::create([
        //         "name" => fake()->name(),
        //         "email" => fake()->email()
        //     ]);
        // }


        // For inserting multiple data of json file in database as shown below :
        $json = File::get(path: "database/json/studentstwo.json");
        // To use sub methods of collect(), we have used collect() as shown below :
        $studentstwo = collect(json_decode($json)); //json_decode() converts json to Array

        // When we create table and if we insert data in table using "create()" method at that time
        // we must have to use "timestamps()" column while creating table because "create()" method
        // needs to add two column values "created_at" and "updated_at" in table that's why we must
        // use "timestamps()" column but if we use old method "insert()" instead of "create()" then
        // there is no need to use "timestamps()" method

        $studentstwo->each(function ($student) {
            studentstwo::create([
                "name" => $student->name,
                "age" => $student->age,
                "city" => $student->city
            ]);
        });
    }
}






Below File is database\seeders\DatabaseSeeder.php File
<?php

namespace Database\Seeders;

use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // User::factory(10)->create();

        // User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);

        $this->call([
            StudentstwoSeeder::class
        ]);
    }
}



Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11