Migration Primary and Foreign key 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->string("email",40)->nullable()->unique();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('students');
}
};
Above File is database/migrations/create_students_table.php FileBelow File is database/migrations/create_libraries_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('libraries', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger("stu_id");
$table->foreign("stu_id")->references("id")->on("students")->onUpdate("cascade")->onDelete("cascade");
$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/migrations/update_library_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::table('libraries', function (Blueprint $table) {
$table->dropForeign("libraries_stu_id_foreign");
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('libraries', function (Blueprint $table) {
//
});
}
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment