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 社區所有。轉載請保留原文鏈接

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章