Laravel migration (数据库迁移) 的使用

简介:迁移就像是数据库的版本控制,允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和 Laravel 的 数据库结构生成器配合使用,让你轻松地构建数据库结构。如果你曾经试过让同事手动在数据库结构中添加字段,那么数据库迁移可以让你不再需要做这样的事情。

来自laravel社区:数据库迁移《Laravel 5.8 中文文档》
具体使用:
1、生成迁移:
使用 Artisan 命令生成迁移文件:

php artisan make:migration create_members_table

迁移文件在 database/migrations下。每个迁移文件名都包含时间戳,以便让 Laravel 确认迁移的顺序。

2、迁移结构
具体字段参考laravle社区:数据库迁移《Laravel 5.8 中文文档》

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMembersTable extends Migration
{
    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::create('members', function (Blueprint $table)
        {
            $table->bigIncrements('id');
            $table->string('u_openid',100)->comment("openid");
            $table->string('u_name',50)->comment('用户昵称');
            $table->string('u_face',255)->comment('用户头像');
            $table->bigInteger('u_integral')->comment('积分');
            $table->string('u_random',30)->comment('用户随机码');
            $table->decimal('u_remainder',10,2)->comment('余额');
            $table->timestamps();
        });
    }

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

3、运行迁移

php artisan migrate
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!