はじめに
モデルの作成
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)