こんなテーブルがある場合に
+----------------+--------------+------+-----+---------------------+-------------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------------------+-------------------------------+
| id | int(7) | NO | PRI | NULL | auto_increment |
| password | varchar(255) | NO | | NULL | |
| user_name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | NO | | current_timestamp() | on update current_timestamp() |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| deleted_at | timestamp | YES | | NULL | |
+----------------+--------------+------+-----+---------------------+-------------------------------+
こんなmigrationファイルを自動で生成してくれます。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTUserInfoTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->integer('id', true)->comment('ユーザーID');
$table->string('password')->comment('パスワード');
$table->string('user_name')->comment('氏名');
$table->string('email')->comment('メールアドレス');
$table->string('remember_token', 100)->nullable()->comment('トークン');
$table->timestamps(10);
$table->softDeletes()->comment('削除日時');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
検証環境
- Windows10
- Laravel 6.20.30
- MariaDB 10.5.11
使用するパッケージ
以下を使用します。
以下のほうがも多いのですが新しいバージョンのLaravel6には対応しておらず使用できませんでした。
laravel5.8以降であれば、上記のものを使用するようにissueに記載があります。
使用方法
インストール
READMEより。
// パッケージのインストール
composer require oscarafdev/migrations-generator --dev
// migrationファイルの生成
php artisan migrate:generate
// migrationファイルを作成してよいかの確認、Yを指定する
> Do you want to log these migrations in the migrations table? [Y/n] :
// 作成するmigrationのバッチ番号の確認、まだmigrationを行っていない場合0を指定する
> Number is: 1. We recommend using Batch Number 0 so that it becomes the "first" migration [Default: 0]
オプション
mysql以外の接続を利用したり、migrationファイルを作成しないテーブルを指定したりできるようです(未検証)。
// mysql(デフォルト)以外の接続を利用
php artisan migrate:generate -c[=CONNECTION]
// 指定したテーブルのmigrationファイルのみ作成
php artisan migrate:generate table1,table2,table3,table4,table5
// 指定したテーブル以外のmigrationファイルのみ作成
php artisan migrate:generate -ignore="table3,table4,table5"
// その他オプションのリストを表示
php artisan help migrate:generate