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 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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