0
0

More than 3 years have passed since last update.

【Laravel】php artisan make:model ***で作成したマイグレーションファイルの中身

Posted at

Laravelのmigrationファイルの中身

<?php

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

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()  //upメソッドでマイグレーション時にこの中身が実行される。
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
        });
    }
//Schema::createでテーブルを作成するらしい。ここでは、productsテーブルが作成されるイメージ
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }

}

Laravelでは、テーブルの中身のカラムとそのデータ型は以下のように記述するらしい。

$table->bigIncrements('id');
$table->timestamps();

これで、テーブル内のカラムとそのデータ型を指定してテーブルを作成する。
しかし、migrationファイルを作成した時点ではまだカラムの情報が不足している場合は、ここで追記が必要。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0