猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

【PHP】Lumen で Migration

f:id:yoshiki_utakata:20200103110401p:plain

Lumen

Laravel を軽くしたと言われる Lumen で Migration をしてみます

artisanコマンドを叩く

Laravel と同じっぽいので Laravel の Eloquent のドキュメントを見ながらやります。

readouble.com

php artisan make:migration create_stars_table

ちなみに、Laravel だと model コマンドがあるのですが、Lumen にはないようなので、下記ページの内容は使えません。

readouble.com

php artisan make:model Star --migration

Migration ファイルを編集

<?php

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

class CreateStarsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stars', function (Blueprint $table) {
            $table->integer('userId');
            $table->string('globalId', 20);
            $table->timestamps();
        });
    }

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

Migration コマンドを叩く

php artisan migrate

Eloquent を有効に

DB に接続するのに Eloquent を使いたいのですが、 Lumen ではデフォルトだと Eloquent がオフになっているので、 bootstrap/app.php$app->withEloquent(); のコメントを消して有効にします。

@@ -23,7 +23,7 @@ $app = new Laravel\Lumen\Application(

 // $app->withFacades();

-// $app->withEloquent();
+$app->withEloquent();

 /*
 |--------------------------------------------------------------------------