【Laravel】artisanのmake:migrationコマンドを解説

Laravel

Laravel 10.xのartisanの「make:migration」コマンドを解説します。

Description

新しいマイグレーションファイルの作成

Usage

php artisan make:migration [--create [CREATE]] [--table [TABLE]] [--path [PATH]] [--realpath] [--fullpath] [--] <name>

Arguments

引数必須説明
name必須マイグレーション名

Options

オプション省略形必須説明
create--作成するテーブル
table--移行するテーブル
path--マイグレーションファイルが作成される場所
realpath--pathで指定したパスが、絶対パスであることを示す
fullpath--マイグレーションのフルパスを出力する(非推奨)

Example

no options

何もない空のマイグレーションファイルが作成されます。

php artisan make:migration foo
   INFO  Migration [database/migrations/2023_06_10_224837_foo.php] created successfully.  
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        //
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        //
    }
};

table

作成するマイグレーションファイルのup()に以下の処理が加わります。

  • テーブル作成
php artisan make:migration --table=bar foo
   INFO  Migration [database/migrations/2023_06_10_231951_foo.php] created successfully.  
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('bar', function (Blueprint $table) {
            //
        });
    }

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

create

作成するマイグレーションファイルのup()に以下の処理が加わります。

  • テーブル作成
  • id、timestamp系カラム(created_at、updated_at)の追加
php artisan make:migration --create=bar foo
   INFO  Migration [database/migrations/2023_06_10_231951_foo.php] created successfully.  
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('bar', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

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

Follow me!

コメント

PAGE TOP
タイトルとURLをコピーしました