Laravel之数据库迁移文件

数据库迁移文件可快速方便的为我们创建数据表

迁移文件位置:

app/database/migrations

创建迁移文件:

php artisan make:migration 文件名(如create_user_table)

运行所有未执行的迁移文件:

php artisan migrate

运行单个迁移文件,例:

php artisan migrate --path=/database/migrations/2021_08_30_072323_create_user_table.php

回滚迁移,如:

php artisan migrate:rollback --path=/database/migrations/2021_08_30_072323_create_user_table.php

迁移文件内容:


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

class CreateUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name',50)->default('')->comment('用户昵称');
            $table->string('phone',11)->default('')->comment('用户手机号');
            $table->string('email',30)->default('')->comment('用户邮件');
            $table->tinyInteger('sex')->default(0)->comment('性别:0-未知,1-男,2-女');
            $table->smallInteger('age')->default(0)->comment('年龄');
            $table->string('birth',10)->default(0)->comment('年龄');
            $table->integer('login_num')->default(0)->comment('登录次数');
            $table->timestamp('last_login_time')->nullable()->comment('最后登录时间');
            $table->timestamp('create_time')->nullable()->comment('注册时间');
            $table->tinyInteger('status')->default(1)->comment('状态:1-正常,2-禁用');
            $table->string('login_ip',15)->default('')->comment('登录ip');

            $table->timestamps();
            $table->softDeletes();
        });

        DB::statement("ALTER TABLE `user` comment '用户表'");
    }

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

你可能感兴趣的:(php,laravel,big,data,php)