Day2day Opportunism

まぁ、平たく言うと「雑記」がだらだらと・・・

Laravel メモ:テーブル定義

テーブル定義についてのメモ
Laravel v10.x
MySQL

まずは、側を作成(ここでは、「flights」テーブルを作成)

php artisan make:migration  create_flights_table

下記のようなファイルが、database/migrations/ に作成される。
2024_03_19_150053_create_flights_table.php

中身はこんな感じ

<?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('flights', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

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

これに、項目の追加と論理名を付けるには…

<?php

    public function up(): void
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('便名');
            $table->string('airline')->comment('航空会社');
            $table->string('airport')->comment('空港');
            $table->dateTime('schedule')->comment('定刻');
            $table->timestamps();
            $table->comment('フライト');
        });
    }

あとは、作るだけ

php artisan migrate

参考
readouble.com