LoginSignup
0
0

More than 3 years have passed since last update.

【Laravel】DBテーブル作成方法

Posted at

1. マイグレーションファイル作成

$ php artisan make:migration create_posts_table
Created Migration: 2020_12_02_092200_create_possts_table

2. マイグレーションファイルを編集

database/migrations/2020_12_02_092200_create_posts_table.php
<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title')->default('');
            $table->text('content')->default(''); 
       $table->timestamps();
        });
    }

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

public function up()の中に書いていく。

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            /* ここから */
            $table->string('title')->default('');
            $table->text('content')->default('');
           /* ここまで */  
       $table->timestamps();
            $table->timestamps();
        });
    }

最後にデータベースに反映

$ php artisan migrate
Migration table created successfully.
Migrating: 2020_12_02_092200_create_posts_table
Migrated:  2020_12_02_092200_create_posts_table
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