laravel中的数据迁移表结构、字段类型、定义整理
生活随笔
收集整理的這篇文章主要介紹了
laravel中的数据迁移表结构、字段类型、定义整理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近使用laravel做項目,其他的用的還算比較熟練,就是在生成遷移文件的時候記不住每個字段類型都怎么表示,下面是整理的一些字段類型:
/* 表引擎 */ $table->engine = 'InnoDB';/* 類型 */ // - 數字 $table->bigInteger('id'); $table->integer('id'); $table->mediumInteger('id'); $table->smallInteger('id'); $table->tinyInteger('id'); $table->decimal('balance', 15, 8); $table->float('balance'); $table->double('balance', 15, 8); $table->real('balance');// - 時間 $table->date('created_at'); $table->dateTime('created_at'); $table->timeStamp('created_at'); $table->time('sunrise'); // - 字符串 $table->char('name', 4); // 等同于 VARCHAR $table->string('name'); // 等同于 VARCHAR(100) $table->string('name', 100); $table->text('description'); $table->mediumText('description'); $table->longText('description');// 等同于 BLOB $table->binary('data'); $table->enum('choices', ['foo', 'bar']); $table->boolean('confirmed'); // - 不經常用的 $table->json('options'); // 等同于數據庫中的 JSON 類型 $table->jsonb('options'); // 等同于數據庫中的 JSONB 類型 $table->uuid('id'); // 等同于數據庫的UUID // 自增ID,類型為 bigint $table->bigIncrements('id');// 自增ID,類型為 int $table->increments('id'); // 添加一個 INTEGER類型的 taggable_id 列和一個 STRING類型的 taggable_type列 $table->morphs('taggable'); // 和 timestamps() 一樣,但允許 NULL 值 $table->nullableTimestamps('created_at'); // 添加一個 'remember_token' 列:VARCHAR(100) NULL $table->rememberToken(); // 添加 'created_at' 和 'updated_at' $table->timeStamps(); // 新增一個 'deleted_at' 列,用于 '軟刪除' $table->softDeletes(); /* 列修改器 */ ->first(); // 將列置于表第一個列(僅限于MYSQL) ->after('列名'); // 將列置于某一列后(僅限于MYSQL) ->nullable(); // 允許列為NULL ->defalut($value); // 指定列默認值 ->unsigned(); // 設置整型列為 UNSIGNED /* 修改列 需安裝 doctrine/dbal,composer require doctrine/dbal */ // change() - 修改列 $table->string('name', 30)->nullable()->change(); // renameColumn() - 重命名列 $table->renameColumn('name', 'title'); /* 刪除列 需安裝 doctrine/dbal,composer require doctrine/dbal */ // 刪除單個列 $table->dropColumn('name'); // 刪除多個列 $table->dropColumn(['name', 'age']); /* 創建索引 * 每種索引,都有3種方式: * 一個string參數 * 一個array參數 - 組合索引 * 2個參數 - 允許自定義索引名 * 注意: * laravel自動分配的索引名: * 表名_列名_索引類型:users_mobile_unique // users表的mobile字段為unique 索引 */ $table->primary('id'); // 主鍵索引 $table->primary(['first', 'last']); // 混合索引(這個不太清楚) $table->primary('first', 'first_primary_index']); // 自定義索引名 $table->unique('mobile'); // 唯一索引 $table->index('state'); // 普通索引 /* 刪除索引 */ $table->dropPrimary('索引名') $table->dropUnique('索引名') $table->dropIndex('索引名') /* 外鍵約束 * 注意: * laravel自動分配的外鍵名: * 表名_列名_foreign:posts_user_id_foreign // posts表的user_id字段添加foreign */ // 添加外鍵,當前表的user_id,外鍵關聯users表的id列 $table->foreign('user_id')->references('id')->on('users'); // 約束 'on delete' 和 'on update' 時,才關聯外鍵 $table->foreign('user_id')->references('id')->on('users')->onDelete; // 刪除外鍵 $table->dropForeign('posts_user_id_foreign');希望對大家有幫助!
總結
以上是生活随笔為你收集整理的laravel中的数据迁移表结构、字段类型、定义整理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI 搜索之战被指反应迟缓,谷歌 CEO
- 下一篇: 解决 Windows 系统使用 Home