Seeders tutorial in Laravel 11

 <?php


namespace Database\Seeders;

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

class StudentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        for ($i = 1; $i <= 10; $i++) {
            student::create([
                "name" => fake()->name(),
                "email" => fake()->unique()->email()
            ]);
        }




        // $json = File::get(path: "database/json/students.json");
        // // To use sub methods of collect(), we have used collect() as shown below :
        // $students = 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

        // $students->each(function ($stu) {
        //     student::create([
        //         "name" => $stu->name,
        //         "email" => $stu->email
        //     ]);
        // });
    }
}
Above File is database/seeders/StudentSeeder.php File





Below File is database/json/students.json File
[
    {
        "name": "Varun Dhavan",
        "email": "varundhavan@gmail.com"
    },
    {
        "name": "Prakash Dave",
        "email": "prakashdave@gmail.com"
    },
    {
        "name": "Ajay Patel",
        "email": "ajaypatel@gmail.com"
    },
    {
        "name" : "Pritesh Patel",
        "email": "priteshpatel@gmail.com"
    }
]





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([
            StudentSeeder::class
        ]);
    }
}





Below File is database/migrations/create_students_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('students', function (Blueprint $table) {
            $table->id();
            $table->string("name", 30);
            $table->string("email", 40)->nullable()->unique();
            $table->timestamps();

            // 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
        });
    }

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



Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11