次に admins テーブルと professors テーブルを作成するためのマイグレーションファイルを生成します.なお,Laravel ではマイグレーションの名前からテーブル名と新しいテーブルを作成しようとしているかを推測します.
vagrant@ubuntu2204 laravelAuth $ php artisan make:migration create_admins_table ⏎ INFO Migration [database/migrations/2023_11_05_111159_create_admins_table.php] created successfully. vagrant@ubuntu2204 laravelAuth $ php artisan make:migration create_professors_table ⏎ INFO Migration [database/migrations/2023_11_05_111213_create_professors_table.php] created successfully. vagrant@ubuntu2204 laravelAuth $
生成されたマイグレーションファイルはそれぞれ次の通りです.
database/migrations/yyyy_mm_dd_hhmmss_create_admins_table.php
<?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('admins', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('admins');
}
};
database/migrations/yyyy_mm_dd_hhmmss_create_professors_table.php
<?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('professors', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('professors');
}
};
これら二つのマイグレーションファイルをそれぞれ次のように編集します.どちらも login_id を使ってログインができるように項目を追加しておきます.
database/migrations/yyyy_mm_dd_hhmmss_create_admins_table.php
<?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('admins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('login_id')->unique();
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('admins');
}
};
database/migrations/yyyy_mm_dd_hhmmss_create_professors_table.php
<?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('professors', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('login_id')->unique();
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('professors');
}
};
マイグレーションを実行して,データベースにテーブルを作成します.
vagrant@ubuntu2204 laravelAuth $ php artisan migrate:status ⏎ Migration name ..................................... Batch / Status 2014_10_12_000000_create_users_table ...................... [1] Ran 2014_10_12_100000_create_password_reset_tokens_table ...... [1] Ran 2019_08_19_000000_create_failed_jobs_table ................ [1] Ran 2019_12_14_000001_create_personal_access_tokens_table ..... [1] Ran 2023_11_03_101930_add_login_id_to_users_table ............. [1] Ran 2023_11_05_111159_create_admins_table ..................... Pending 2023_11_05_111213_create_professors_table ................. Pending vagrant@ubuntu2204 laravelAuth $ php artisan migrate ⏎ INFO Running migrations. 2023_11_05_111159_create_admins_table ................... 23ms DONE 2023_11_05_111213_create_professors_table ............... 16ms DONE vagrant@ubuntu2204 laravelAuth $ php artisan migrate:status ⏎ Migration name ..................................... Batch / Status 2014_10_12_000000_create_users_table ...................... [1] Ran 2014_10_12_100000_create_password_reset_tokens_table ...... [1] Ran 2019_08_19_000000_create_failed_jobs_table ................ [1] Ran 2019_12_14_000001_create_personal_access_tokens_table ..... [1] Ran 2023_11_03_101930_add_login_id_to_users_table ............. [1] Ran 2023_11_05_111159_create_admins_table ..................... [2] Ran 2023_11_05_111213_create_professors_table ................. [2] Ran vagrant@ubuntu2204 laravelAuth $