Laravel 7.x 下使用artisan migrate 迁移表功能中 Schema类 创建字段和索引

创建表

Schema::create('users', function (Blueprint $table) {
            $table->increments('ID');
            $table->string('UserName',250)->unique();
            $table->string('PassWord',250);
            $table->text('Introduction')->nullable(); // 介绍
            $table->string('Email',250)->unique()->nullable();
            $table->integer('Phone')->unique()->nullable();
            $table->string('ProfilePhoto')->nullable();
            $table->integer('AddUserID');
            $table->integer('AddTime');
            $table->integer('UpdateUserID');
            $table->integer('UpdateTime');
        });
$table->id();	相当于 $table->bigIncrements('id')
$table->foreignId('user_id');	相当于 $table->unsignedBigInteger('user_id')
$table->bigIncrements('id');	递增 ID(主键),相当于「UNSIGNED BIG INTEGER」
$table->bigInteger('votes');	相当于 BIGINT
$table->binary('data');	相当于 BLOB
$table->boolean('confirmed');	相当于 BOOLEAN
$table->char('name', 100);	相当于带有长度的 CHAR
$table->date('created_at');	相当于 DATE
$table->dateTime('created_at', 0);	相当于 DATETIME
$table->dateTimeTz('created_at', 0);	相当于带时区 DATETIME
$table->decimal('amount', 8, 2);	相当于带有精度与基数 DECIMAL
$table->double('amount', 8, 2);	相当于带有精度与基数 DOUBLE
$table->enum('level', ['easy', 'hard']);	相当于 ENUM
$table->float('amount', 8, 2);	相当于带有精度与基数 FLOAT
$table->geometry('positions');	相当于 GEOMETRY
$table->geometryCollection('positions');	相当于 GEOMETRYCOLLECTION
$table->increments('id');	递增的 ID (主键),相当于「UNSIGNED INTEGER」
$table->integer('votes');	相当于 INTEGER
$table->ipAddress('visitor');	相当于 IP 地址
$table->json('options');	相当于 JSON
$table->jsonb('options');	相当于 JSONB
$table->lineString('positions');	相当于 LINESTRING
$table->longText('description');	相当于 LONGTEXT
$table->macAddress('device');	相当于 MAC 地址
$table->mediumIncrements('id');	递增 ID (主键) ,相当于「UNSIGNED MEDIUM INTEGER」
$table->mediumInteger('votes');	相当于 MEDIUMINT
$table->mediumText('description');	相当于 MEDIUMTEXT
$table->morphs('taggable');	相当于加入递增的 taggable_id 与字符串 taggable_type
$table->uuidMorphs('taggable');	相当于加入 taggable_id 与字符串 taggable_typeUUID 列。
$table->multiLineString('positions');	相当于 MULTILINESTRING
$table->multiPoint('positions');	相当于 MULTIPOINT
$table->multiPolygon('positions');	相当于 MULTIPOLYGON
$table->nullableMorphs('taggable');	相当于可空版本的 morphs () 字段
$table->nullableUuidMorphs('taggable');	相当于可空版本的 uuidMorphs() 字段
$table->nullableTimestamps(0);	timestamps() 方法别名。
$table->point('position');	相当于 POINT
$table->polygon('positions');	相当于 POLYGON
$table->rememberToken();	相当于可空版本的 VARCHAR (100) 的 remember_token 字段
$table->set('flavors', ['strawberry', 'vanilla']);	相当于 SET
$table->smallIncrements('id');	递增 ID(主键),相当于「UNSIGNED SMALLINT」
$table->smallInteger('votes');	相当于 SMALLINT
$table->softDeletes(0);	相当于为软删除添加一个可空的 deleted_at 字段
$table->softDeletesTz(0);	相当于为软删除添加一个可空的 带时区的 deleted_at 字段
$table->string('name', 100);	相当于带长度的 VARCHAR
$table->text('description');	相当于 TEXT
$table->time('sunrise', 0);	相当于 TIME
$table->timeTz('sunrise', 0);	相当于带时区的 TIME
$table->timestamp('added_on', 0);	相当于 TIMESTAMP
$table->timestampTz('added_on', 0);	相当于带时区的 TIMESTAMP
$table->timestamps(0);	相当于可空的 created_at 和 updated_at TIMESTAMP
$table->timestampsTz(0);	相当于可空且带时区的 created_at 和 updated_atTIMESTAMP
$table->tinyIncrements('id');	相当于自动递增 UNSIGNED TINYINT
$table->tinyInteger('votes');	相当于 TINYINT
$table->unsignedBigInteger('votes');	相当于 Unsigned BIGINT
$table->unsignedDecimal('amount', 8, 2);	相当于带有精度和基数的 UNSIGNED DECIMAL
$table->unsignedInteger('votes');	相当于 Unsigned INT
$table->unsignedMediumInteger('votes');	相当于 Unsigned MEDIUMINT
$table->unsignedSmallInteger('votes');	相当于 Unsigned SMALLINT
$table->unsignedTinyInteger('votes');	相当于 Unsigned TINYINT
$table->uuid('id');	相当于 UUID
$table->year('birth_year');	相当于 YEAR

————————————————
原文作者:Laravel China 社区文档:《Laravel 7 中文文档(7.x)》
转自链接:https://learnku.com/docs/laravel/7.x/migrations/7496
版权声明:翻译文档著作权归译者和 LearnKu 社区所有。转载请保留原文链接

创建索引



$table->primary('id');	//添加主键索引
$table->primary(['id', 'parent_id']);	//添加复合索引
$table->unique('email');	//添加唯一索引
$table->unique('state', 'my_index_name'); // 指定自定义索引名称,第二个参数是索引自定义名称,没有larave会自动生成
$table->index('state');	//添加普通索引
$table->spatialIndex('location');	//添加空间索引(不支持 SQLite)

————————————————
原文作者:Laravel China 社区文档:《Laravel 7 中文文档(7.x)》
转自链接:https://learnku.com/docs/laravel/7.x/migrations/7496
版权声明:翻译文档著作权归译者和 LearnKu 社区所有。转载请保留原文链接
发布了14 篇原创文章 · 获赞 3 · 访问量 4390

猜你喜欢

转载自blog.csdn.net/kina100/article/details/105522653