フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

Laravel 問い合わせフォームを作成する①

はじめに

  • laravelで問い合わせフォームを開発する

モデルの作成

  • モデルを作成する
php artisan make:model ContactForm -m

マイグレーション(テーブルの作成)

  • /database/migrations/YYYY_MM_DD_HHMMSS_create_contact_forms_table.phpファイルを下記のように修正し問い合わせテーブルのフィールドを追加する
<?php

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

class CreateContactFormsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact_forms', function (Blueprint $table) {
            $table->id();
            $table->string('name', 20);
            $table->string('email', 255);
            $table->longText('url')->nullable();
            $table->boolean('sex');
            $table->tinyInteger('age');
            $table->text('description');
            $table->timestamps();
        });
    }

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

readouble.com

  • テーブルを作成する
$ php artisan migrate
  • テーブルが作成されていることを確認する
mysql> desc contact_forms;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(20)         | NO   |     | NULL    |                |
| email       | varchar(255)        | NO   |     | NULL    |                |
| url         | longtext            | YES  |     | NULL    |                |
| sex         | tinyint(1)          | NO   |     | NULL    |                |
| age         | tinyint(4)          | NO   |     | NULL    |                |
| description | text                | NO   |     | NULL    |                |
| created_at  | timestamp           | YES  |     | NULL    |                |
| updated_at  | timestamp           | YES  |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
  • 作成したテーブルにカラムを追加する場合、table名にフィールドを追加したいカラム名を指定する
php artisan make:migration add_title_to_contact_forms_table --table=contact_forms
  • /database/migrations/YYYY_MM_DD_HHMMSS_add_title_to_contact_forms_table.phpファイルを下記のように修正する
<?php

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

class CreateContactFormsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact_forms', function (Blueprint $table) {
            $table->id();
            $table->string('name', 20);
            $table->string('email', 255);
            $table->longText('url')->nullable();
            $table->boolean('sex');
            $table->tinyInteger('age');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contact_forms');
    }
}
  • 既存テーブルにカラムを追加する
$ php artisan migrate
  • migrateのステータスを確認することができる。migrate:rollbackで1つ前のマイグレーションされた状態に戻すことができる
$ php artisan migrate:status
+------+----------------------------------------------------+-------+
| Ran? | Migration                                          | Batch |
+------+----------------------------------------------------+-------+
| Yes  | 2020_10_14_232814_create_contact_forms_table       | 1     |
| Yes  | 2020_10_14_235306_add_title_to_contact_forms_table | 2     |
+------+----------------------------------------------------+-------+
  • カラムが追加されていることを確認する
mysql> desc contact_forms;
+-------------+---------------------+------+-----+---------+----------------+
| Field       | Type                | Null | Key | Default | Extra          |
+-------------+---------------------+------+-----+---------+----------------+
| id          | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(20)         | NO   |     | NULL    |                |
| email       | varchar(255)        | NO   |     | NULL    |                |
| url         | longtext            | YES  |     | NULL    |                |
| sex         | tinyint(1)          | NO   |     | NULL    |                |
| age         | tinyint(4)          | NO   |     | NULL    |                |
| description | text                | NO   |     | NULL    |                |
| subject     | varchar(50)         | NO   |     | NULL    |                |
| created_at  | timestamp           | YES  |     | NULL    |                |
| updated_at  | timestamp           | YES  |     | NULL    |                |
+-------------+---------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)