Build: Database setup
This commit is contained in:
35
database/migrations/2025_01_25_190302_create_roles_table.php
Normal file
35
database/migrations/2025_01_25_190302_create_roles_table.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?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()
|
||||||
|
{
|
||||||
|
Schema::create('roles', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Insert predefined roles
|
||||||
|
// DB::table('roles')->insert([
|
||||||
|
// ['name' => 'Admin'],
|
||||||
|
// ['name' => 'Coordinator'],
|
||||||
|
// ['name' => 'Faculty']
|
||||||
|
// ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('roles');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<?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()
|
||||||
|
{
|
||||||
|
Schema::create('departments', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Insert predefined departments
|
||||||
|
// DB::table('departments')->insert([
|
||||||
|
// ['name' => 'Computer Engineering'],
|
||||||
|
// ['name' => 'Information Technology'],
|
||||||
|
// ['name' => 'Mechanical Engineering'],
|
||||||
|
// ['name' => 'Electrical Engineering'],
|
||||||
|
// ['name' => 'Civil Engineering']
|
||||||
|
// ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('departments');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?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()
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->unsignedBigInteger('role_id')->after('id');
|
||||||
|
$table->unsignedBigInteger('department_id')->nullable()->after('role_id');
|
||||||
|
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
|
||||||
|
$table->foreign('department_id')->references('id')->on('departments')->onDelete('cascade');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('users', function (Blueprint $table) {
|
||||||
|
$table->dropForeign(['role_id']);
|
||||||
|
$table->dropForeign(['department_id']);
|
||||||
|
$table->dropColumn(['role_id', 'department_id']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -15,9 +15,14 @@ class DatabaseSeeder extends Seeder
|
|||||||
{
|
{
|
||||||
// User::factory(10)->create();
|
// User::factory(10)->create();
|
||||||
|
|
||||||
User::factory()->create([
|
$this->call([
|
||||||
'name' => 'Test User',
|
RoleSeeder::class,
|
||||||
'email' => 'test@example.com',
|
DepartmentSeeder::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// User::factory()->create([
|
||||||
|
// 'name' => 'Test User',
|
||||||
|
// 'email' => 'test@example.com',
|
||||||
|
// ]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
database/seeders/DepartmentSeeder.php
Normal file
22
database/seeders/DepartmentSeeder.php
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class DepartmentSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
DB::table('roles')->insert([
|
||||||
|
['name' => 'Admin'],
|
||||||
|
['name' => 'Coordinator'],
|
||||||
|
['name' => 'Faculty'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
database/seeders/RoleSeeder.php
Normal file
24
database/seeders/RoleSeeder.php
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
class RoleSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
DB::table('departments')->insert([
|
||||||
|
['name' => 'Computer Engineering'],
|
||||||
|
['name' => 'Information Technology'],
|
||||||
|
['name' => 'Mechanical Engineering'],
|
||||||
|
['name' => 'Electrical Engineering'],
|
||||||
|
['name' => 'Civil Engineering'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user