今回は前回に続けて開発を進めていきましょう。
前章をまだご覧になっていない方は以下から開発を始めましょう。
3 章:マイグレーション
DB 設計に基づいてマイグレーションを作成していきましょう。
必要なテーブルを順に作成していきます。make:migration
Artisanコマンドを使いマイグレーションを生成していきましょう。
laradock $ docker-compose exec --user=laradock workspace bash /var/www$ php artisan make:migration create_threads_table Created Migration: 2021_04_13_081530_create_threads_table /var/www$ php artisan make:migration create_admins_table Created Migration: 2021_04_13_081543_create_adminds_table /var/www$ php artisan make:migration create_messages_table Created Migration: 2021_04_13_081550_create_messages_table l/var/www$ php artisan make:migration create_images_table Created Migration: 2021_04_13_081557_create_images_table
uses テーブル用の migration は Laravel install 時に作成されているので他の 4 migration を作成しました。
2021_04_13_081530_create_threads_table.php // 省略 Schema::create('threads', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('user_id'); $table->string('name'); $table->boolean('is_user_checked'); $table->timestamp('latest_comment_time'); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users'); }); // 省略
2021_04_13_081543_create_adminds_table.php // 省略 Schema::create('admins', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); // 省略
2021_04_13_081550_create_messages_table.php // 省略 Schema::create('messages', function (Blueprint $table) { $table->bigIncrements('id'); $table->text('body'); $table->unsignedBigInteger('thread_id'); $table->unsignedBigInteger('user_id'); $table->timestamps(); $table->foreign('thread_id')->references('id')->on('threads'); $table->foreign('user_id')->references('id')->on('users'); }); // 省略
2021_04_13_081557_create_images_table.php // 省略 Schema::create('images', function (Blueprint $table) { $table->bigIncrements('id'); $table->unsignedBigInteger('message_id'); $table->text('s3_file_path'); $table->timestamps(); $table->foreign('message_id')->references('id')->on('messages'); }); // 省略
では次に Laradock で作成した MySQL とLaravel を接続していきましょう。
2ch の .env を確認してください。( laradock 配下の .env ではありません )
.env DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=2ch DB_USERNAME=default DB_PASSWORD=root
Laradock の .env で設定した、DATABASE, USERNAME, PASSWORD を設定しました。
この設定をすることで、DB の接続が完了します。
laradock $ docker-compose exec --user=laradock workspace bash /var/www# $ php artisan migrate Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (0.05 seconds) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (0.04 seconds) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (0.02 seconds) Migrating: 2021_04_13_081530_create_threads_table Migrated: 2021_04_13_081530_create_threads_table (0.07 seconds) Migrating: 2021_04_13_081543_create_admins_table Migrated: 2021_04_13_081543_create_admins_table (0.07 seconds) Migrating: 2021_04_13_081550_create_messages_table Migrated: 2021_04_13_081550_create_messages_table (0.1 seconds) Migrating: 2021_04_13_081557_create_images_table Migrated: 2021_04_13_081557_create_images_table (0.07 seconds)
3 章での変更点は以下からご確認いただけます。
https://github.com/t-aburasoba/thread-board/pull/1/files
4 章:ログイン機能(認証)
認証機能をつけていきましょう。
ドキュメントを参考に実装していきます。
https://readouble.com/laravel/6.x/ja/authentication.html
laradock $ docker-compose exec --user=laradock workspace bash /var/www# $ composer require laravel/ui "^1.0" --dev /var/www# $ php artisan ui vue --auth /var/www# $ npm install /var/www# $ npm run dev
非常に簡単ですね。
それでは、実際にログイン機能が実装できたのかを確認していきましょう。

右上に「Login」「Register」が表示されました。
ここまでで、ログイン機能が実装できていますので認証してみましょう!

4 章での変更点は以下からご確認いただけます。
https://github.com/t-aburasoba/thread-board/pull/3/files
コメントを残す