Laravel 10 入門トップページ


目次

  1. 全体の概要
  2. Laravel によるユーザ認証
  3. ユーザ認証を備えたコメント掲示板の開発
  4. マルチ認証の実現
    1. 概要
    2. 準備作業
    3. コントローラの作成
    4. ガードの追加
    5. モデルの作成
    6. データベースのマイグレーション
    7. シーダによる管理者・教員データの登録
    8. コントローラの生成
    9. ガードごとに認証不要のダッシュボードを作成
    10. ログイン機能実装の手順
    11. モデルを認証必須に
    12. リクエストの作成
    13. ログインコントローラの作成
    14. ルートの定義
    15. ログインフォームの作成
    16. コンポーネントの作成
    17. レイアウトの作成
    18. ナビゲーションの作成
    19. コントローラの編集
    20. ビューの作成
    21. ミドルウェアの修正
    22. ルートの定義
    23. 管理者ログインの動作確認
    24. 管理者と教員ページのデザイン変更

Laravel でユーザ認証とマルチ認証を実現する

マルチ認証の実現

データベースのマイグレーション

次に 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 $

目次に戻る