Laravel框架相关

资料2 - PHP Artisan Migration 数据库表

2017-01-22  本文已影响46人  7c03aed0f01f

Artisan Migration 数据库表结构

  1. Migration 常用命令
php artisan migrate                                           # 执行迁移
php artisan migrate --force                                   # 强制迁移,可能会造成数据丢失
php artisan migrate:rollback                                  # 回滚迁移/撤销migrate操作
php atrisan make:migrate                                      # 对之前的命令基础上 再做操作
php artisan make:migration create_news_table --create=news    # 创建 news migration 文件
                                                                 # 生成 /database/migrations/2014_10_12_000000_create_users_table.php
php artisan make:migration add_auther_column_to_news          # 用于后续操作
  1. Migration 字段类型
# b
$table->bigIncrements('id');              # 递增 ID(主键),相当于「UNSIGNED BIG INTEGER」型态。
$table->bigInteger('votes');              # 相当于 BIGINT 型态。
$table->binary('data');                   # 相当于 BLOB 型态。
$table->boolean('confirmed');             # 相当于 BOOLEAN 型态。
# c
$table->char('name', 4);                  # 相当于 CHAR 型态,并带有长度。
# d
$table->date('created_at');               # 相当于 DATE 型态。
$table->dateTime('created_at');           # 相当于 DATETIME 型态。
$table->decimal('amount', 5, 2);          # 相当于 DECIMAL 型态,并带有精度与基数。
$table->double('column', 15, 8);          # 相当于 DOUBLE 型态,总共有 15 位数,在小数点后面有 8 位数。
# e
$table->enum('choices', ['foo', 'bar']);  # 相当于 ENUM 型态。
# f
$table->float('amount');                  # 相当于 FLOAT 型态。
# i
$table->increments('id');                 # 递增的 ID (主键),使用相当于「UNSIGNED INTEGER」的型态。
$table->integer('votes');                 # 相当于 INTEGER 型态,长度11。
# j
$table->json('options');                  # 相当于 JSON 型态。
$table->jsonb('options');                 # 相当于 JSONB 型态。
# l
$table->longText('description');          # 相当于 LONGTEXT 型态。
# m
$table->mediumInteger('numbers');         # 相当于 MEDIUMINT 型态。
$table->mediumText('description');        # 相当于 MEDIUMTEXT 型态。
$table->morphs('taggable');               # 加入整数 taggable_id 与字符串 taggable_type
# n
$table->nullableTimestamps();             # 与 timestamps() 相同,但允许为 NULL。
# r
$table->rememberToken();                  # 加入 remember_token 并使用 VARCHAR(100) NULL。
# s
$table->smallInteger('votes');            # 相当于 SMALLINT 型态。
$table->softDeletes();                    # 加入 deleted_at 字段用于软删除操作。
$table->string('email');                  # 相当于 VARCHAR 型态。
$table->string('name', 100);              # 相当于 VARCHAR 型态,并带有长度。
# t
$table->text('description');              # 相当于 TEXT 型态。
$table->time('sunrise');                  # 相当于 TIME 型态。
$table->tinyInteger('numbers');           # 相当于 TINYINT 型态,长度4。
$table->timestamp('added_on');            # 相当于 TIMESTAMP 型态。
$table->timestamps();                     # 加入 created_at 和 updated_at 字段。
# u
$table->uuid('id');                       # 相当于 UUID 型态。
  1. Migration 字段类型 修饰符
->first()          # 将此字段放置在数据表的「第一个」(仅限 MySQL)
->after('column')  # 将此字段放置在其它字段「之后」(仅限 MySQL)
->nullable()       # 此字段允许写入 NULL 值
->default($value)  # 为此字段指定「默认」值
->unsigned()       # 设置 integer 字段为 UNSIGNED,禁用负值
  1. Migration 字段类型 可用索引
$table->primary('id');              # 加入主键。
$table->primary(['first', 'last']);   # 加入复合键。
$table->unique('email');              # 加入唯一索引。
$table->index('state');            # 加入基本索引。
  1. 常用
// increments = unsignedInteger 不为负值的整型 + autoIncrement 自增
$table->increments('id');
// 设置int类型,禁用负值,加入基本索引
$table->integer('userId')->unsigned()->index()->comment('用户ID');
// 设置外键约束
$table->foreign('userId')->references('id')->on('users')->onDelete('cascade'); 

追加字段

// phone 新增的字段,column 列的意思,users 表名
php artisan make:migration add_phone_column_to_users --table=users

写新增的字段

    public function up() {
        Schema::create('users', function (Blueprint $table) {
            $table->string('phone')->comment('用户手机号');
        });
    }

案例

  1. users表
Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('mobile',32)->comment('手机号');
    $table->string('cashNum', 60)->default('')->comment('支付账号');
    $table->string('password')->comment('密码');
    $table->string('tpwd')->comment('支付密码');
    $table->string('userIcon')->default('')->nullable()->comment('用户头像');
    $table->string('shopName')->default('')->comment('店铺名称');
    $table->string('recommen')->default('')->comment('推荐人');
    $table->decimal('balance',12,2)->default(0.00)->comment('余额');
    $table->rememberToken();
    $table->timestamps();
});
  1. aaa
    public function up() {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->unsigned()->comment('外键');
            $table->string('title')->comment('标题');
            $table->string('slug')->unique()->index()->comment('锚点');
            $table->string('summary')->comment('概要');
            $table->text('content')->comment('内容');
            $table->text('origin')->comment('文章来源');
            $table->integer('comment_count')->unsigned()->comment('评论次数');
            $table->integer('view_count')->unsigned()->comment('浏览次数');
            $table->integer('favorite_count')->unsigned()->comment('点赞次数');
            $table->boolean('published')->comment('文章是否发布');
            $table->timestamps();
            //Post表中category_id字段作为外键,与Category一对多关系
            $table->foreign('category_id')
                  ->references('id')
                  ->on('categories')
                  ->onUpdate('cascade')
                  ->onDelete('cascade');
        });
    }
  1. aaa
上一篇下一篇

猜你喜欢

热点阅读