LoginSignup
0
0

More than 3 years have passed since last update.

#4 データベース構築 ~laravel×AWSで掲示板を作ろう~

Last updated at Posted at 2020-11-07

laravel×AWSで掲示板を作ろう

本記事は、laravel×AWSの第4回です。
作ったサイト:https://vible.jp

【参照】
1. 完成形イメージ
2. AWSサーバを立てる(工事中)
3. サーバにlaravelを導入してRDSと連携する(工事中)
4. データベース構築
5. Modelの作成
6. Controllerの作成
7. いいね機能の実装
8. ルーティングの実装

データベース構築が必要となる機能

本サービスにおいてデータベース管理が必要となる機能は以下となります。
ユーザ情報管理に関しては、laravelが用意してくれているのでそのまま使っています。(laravel便利ですね)

アプリでの役割 作成するテーブル
悩み投稿 questions
悩みアドバイス投稿 answer_questions
ツイート投稿 tweets
ツイートへのコメント投稿 answer_tweets
いいね投稿 question_likes, answer_question_likes, tweet_likes, answer_tweet_like

questionsテーブル作成

ここではquestionsテーブルの作成を例として説明していきます。他のテーブルを作成する際も同様ですので参考にしてください。

まずはmigrationファイルを作成します。
これはデータベースの「素」となるファイルと思ってください。

$ php artisan make:migration Question 

すると「日付 + questions_table.php」というファイルが作成されます。(※laravelのルールでテーブル名は必ず複数形)
これがテーブルの「素」となります。ここにテーブルの構造を追記していきましょう。今回questionsテーブルの構造は以下の表のようにします。

項目名 概要
id 各質問に与えられる投稿ID
title 質問のタイトル
content 質問内容
user_id 投稿者のID
created_at 作成日時
updated_at 更新日時

なので、migrationファイルを以下のように編集してあげればOK。

2020_10_10_045014_create_questions_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateQuestionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('content');
            $table->unsignedBigInteger('user_id');
            $table->timestamps();
        });
    }

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

あとは以下を実行すればテーブルがめでたく作成されます。

$ php artisan migrate 

※他のテーブルについてはgithubを参照お願いします。(database\migrationsに格納されています)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0