Laravel php artisan make:migration

Posted by

创建表 credit_currency 使用下面方法:

php artisan make:migration create_credit_currency_table
Created Migration: 2022_06_23_055345_create_credit_currency_table

我们发现在 laravel8/database/migrations/下面自动创建了2022_06_23_055345_create_credit_currency_table.php 。

<?php

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

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

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

现在我们要为表添加几个自带如下:

   $table->string('credit_code', 150);
   $table->string('currency', 150);
   $table->float('rate', 5, 2)->default(1);
   $table->boolean('status')->default(1);

添加完成之后,完整代码如下:

<?php

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

class CreateCreditCurrencyTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('credit_currency', function (Blueprint $table) {
            $table->id();
            $table->string('credit_code', 150);
            $table->string('currency', 150);
            $table->float('rate', 5, 2)->default(1);
            $table->boolean('status')->default(1);
            $table->timestamps();
        });
    }

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

最后我们去运行下面命令,以便完成表字段的创建。

php artisan migrate 或者后面跟完成的文件路径。

yshuqdeMacBook-Pro:laravel8 yshuq$ php artisan migrate --path=./database/migrations/2022_06_23_055345_create_credit_currency_table.php
Migrating: 2022_06_23_055345_create_credit_currency_table
Migrated:  2022_06_23_055345_create_credit_currency_table (49.45ms)
You have new mail in /var/mail/yshuq
yshuqdeMacBook-Pro:laravel8 yshuq$