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 协议》,转载必须注明作者和本文链接