Laravel 10 入門トップページ


目次

  1. 全体の概要
  2. Laravel によるユーザ認証
  3. ユーザ認証を備えたコメント掲示板の開発
    1. 概要
    2. データベースのマイグレーション
    3. シーダによるコメントデータの登録
    4. モデルとコントローラの生成
    5. ルートの定義と確認
    6. 未検証ユーザの動作検証
    7. コメントの一覧表示
    8. ナビゲーションメニューの追加
    9. コメント一覧の Tailwind CSS によるスタイリング
    10. コメントの詳細ページ
    11. リレーションシップの設定と投稿者名の表示
    12. ページネーションの作成
    13. コメントの投稿
    14. コメントの編集
    15. コメントの削除
    16. ナビゲーションのハイライトを調整
  4. マルチ認証の実現

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

ユーザ認証を備えたコメント掲示板の開発

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

コメント掲示板を作成するために,まず投稿されたコメントデータを格納する comments テーブルをデータベースに作成します.Laravel ではテーブルをマイグレーションファイルを実行することによって作成します.そのマイグレーションファイルを生成します.この時,Laravel はマイグレーションの名前からテーブル名と新しいテーブルを作成しようとしているかを推測します.

vagrant@ubuntu2204 laravelAuth $ php artisan make:migration create_comments_table ⏎

   INFO  Migration [database/migrations/2023_11_04_164822_create_comments_table.php] created successfully.

vagrant@ubuntu2204 laravelAuth $

上のコマンドで comments テーブルを作成するためのマイグレーションファイルが次のとおり生成されました.

database/migrations/yyyy_mm_dd_hhmmss_create_comments_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('comments', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

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

テーブル comments には件名 (title) と本文 (body) に加えて,投稿ユーザの ID (user_id) の属性を持たせます.もちろん,user_id は users テーブルの主キーを参照する外部キーになります.このリレーションシップに関する詳細はこちらを参照してください.

database/migrations/yyyy_mm_dd_hhmmss_create_comments_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('comments', function (Blueprint $table) {
            $table->id();
            $table->string('title', 255);
            $table->text('body');
            $table->unsignedBigInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')
                  ->references('id')
                  ->on('users')
                  ->onDelete('cascade');
        });
    }

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

マイグレーションを実行してテーブルを生成します.

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_04_164822_create_comments_table ................... Pending

vagrant@ubuntu2204 laravelAuth $ php artisan migrate ⏎

   INFO  Running migrations.

  2023_11_04_164822_create_comments_table ................. 12ms 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_04_164822_create_comments_table ................... [2] Ran

vagrant@ubuntu2204 laravelAuth $

sqlite> select * from users; ⏎
id|name|email|email_verified_at|password|remember_token|created_at|updated_at|login_id|student_id
1|A. Sample|a@sample.com|2023-11-03 00:05:00|$2y$12$1o9lB/bK29D/ctQ31oqR4OWPbuwMfYFREeE1C3mMqoeZisWnlImF6||2023-11-03 00:01:01|2023-11-03 00:01:01|user_a|6300997
2|B. Sample|b@sample.com|2023-11-03 00:06:00|$2y$12$FDhbZGy0w7vLtbODkmhhuu/oOKmGzfxOx/.nuM6LHK6QWlFWfTgCO||2023-11-03 00:02:01|2023-11-03 00:02:01|user_b|6300998
3|C. Sample|c@sample.com|2023-11-03 00:07:00|$2y$12$RjPnebCTS6jUax8s7cE5ve2126fKorZ8XFIRxuo3dMGn7ilP1ahBu||2023-11-03 00:03:01|2023-11-03 00:03:01|user_c|6300999
sqlite>
sqlite> .tables ⏎
comments                migrations              personal_access_tokens
failed_jobs             password_reset_tokens   users
sqlite>
sqlite> .schema comments ⏎
CREATE TABLE IF NOT EXISTS "comments" (
    "id" integer primary key autoincrement not null,
    "title" varchar not null,
    "body" text not null,
    "user_id" integer not null,
    "created_at" datetime,
    "updated_at" datetime,
    foreign key("user_id") references "users"("id") on delete cascade
    );
sqlite>

目次に戻る