Modal Factory tutorial in Laravel 11

 <?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->integer("age");
            $table->string("email", 40)->nullable()->unique();
            $table->string("address");
            $table->string("city");
            $table->string("phone");
            $table->string("password");
            $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');
    }
};
Above File is database/migrations/create_students_table.php File





Below File is database/factories/StudentFactory.php File
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Student>
 */
class StudentFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            "name" => fake()->name(),
            "age" => fake()->numberBetween(15, 20),
            "email" => fake()->email(),
            "address" => fake()->address(),
            "city" => fake()->city(),
            "phone" => fake()->phoneNumber(),
            "password" => fake()->password()
        ];
    }
}






Below File is database/seeders/StudentSeeder.php File
<?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
    {
        student::factory(5)->create();

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




Below File is database/seeders/DatabaseSeeders.php File
<?php

namespace Database\Seeders;

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

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',
        // ]);

        // student::factory()->count(5)->create();

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





Below File is app/Models/student.php File
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class student extends Model
{
    use HasFactory;
}






Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Eloquent with JSON Data Columns Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11