Migration Primary and Foreign key Tutorial in Laravel 11

 <?php


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    public $timestamps = false;
}
Above File is app\Models\Student File





Below File is app\Models\Library File
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Library extends Model
{
    public $timestamps = false;
}





Below File is database\json\students File
[
    {
        "name": "Amitabh Bachchan",
        "email": "amitabh@gmail.com"
    },
    {
        "name": "Salman Khan",
        "email": "salman@gmail.com"
    },
    {
        "name": "Shahid Kapoor",
        "email": "shahid@gmail.com"
    }
]





Below File is database\json\libraries File
[
    {
        "stu_id": 1,
        "book": "Book One",
        "due_date": "2023-06-30",
        "status": 1
    },
    {
        "stu_id": 3,
        "book": "Book Two",
        "due_date": "2023-06-28",
        "status": 1
    },
    {
        "stu_id": 2,
        "book": "Book Three",
        "due_date": "2023-06-27",
        "status": 1
    }
]





Below File is database\migrations\2024_12_10_045952_create_students_table 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();
        });
    }

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





Below File is database\migrations\2024_12_10_052531_create_libraries_table 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('libraries', function (Blueprint $table) {
            // First way for making Foreign key as shown below :
            $table->id();
            $table->unsignedBigInteger("stu_id");    //Below is the First way for making Foreign key
            $table->foreign("stu_id")->references("id")->on("students")->onDelete("cascade")->onUpdate("cascade");
            $table->string("book");
            $table->date("due_date")->nullable();
            $table->boolean("status");


            // Second way for making Foreign key as shown below :
            // $table->id();
            // $table->foreignId("stu_id")->constrained("students")->onUpdate("cascade")->onDelete("cascade");   //Second way for making Foreign key
            // $table->string("book");
            // $table->date("due_date")->nullable();
            // $table->boolean("status");


        });
    }

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





Below File is database\seeders\StudentSeeder File
<?php

namespace Database\Seeders;

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

class StudentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $json = File::get(path: "database/json/students.json");
        $students = collect(json_decode($json));

        $students->each(function ($student) {
            Student::create([
                "name" => $student->name,
                "email" => $student->email
            ]);
        });
    }
}





Below File is database\seeders\LibrarySeeder File
<?php

namespace Database\Seeders;

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

class LibrarySeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $json = File::get(path: "database/json/libraries.json");
        $libraries = collect(json_decode($json));

        $libraries->each(function ($library) {
            Library::create([
                "stu_id" => $library->stu_id,
                "book" => $library->book,
                "due_date" => $library->due_date,
                "status" => $library->status
            ]);
        });
    }
}





Below File is database\seeders\DatabaseSeeder 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,
            LibrarySeeder::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