laravel框架 數據庫遷移 (一)創建列

首先在命令行創建一個遷移文件
php artisan make:migration create_testpaper_table在這裏插入圖片描述
打開database\migrations文件夾下的新創建好的php文件可以看到已經配置了一些東西在這裏插入圖片描述
要運行應用中所有未執行的遷移,可以使用 Artisan 命令提供的migrate方法:

php artisan migrate

在命令行輸入上面指令就可以在數據庫創建出表來了

可用的列類型

命令 描述
$table->bigIncrements(‘id’); 自增ID,類型爲bigint
$table->bigInteger(‘votes’); 等同於數據庫中的BIGINT類型
$table->binary(‘data’); 等同於數據庫中的BLOB類型
$table->bigIncrements(‘id’); 自增ID,類型爲bigint
$table->boolean(‘confirmed’); 等同於數據庫中的BOOLEAN類型
$table->char(‘name’, 4); 等同於數據庫中的CHAR類型
$table->date(‘created_at’); 等同於數據庫中的DATE類型
$table->dateTime(‘created_at’); 等同於數據庫中的DATETIME類型
$table->dateTimeTz(‘created_at’); 等同於數據庫中的DATETIME類型(帶時區)
$table->decimal(‘amount’, 5, 2); 等同於數據庫中的DECIMAL類型,帶一個精度和範圍
$table->double(‘column’, 15, 8); 等同於數據庫中的DOUBLE類型,帶精度, 總共15位數字,小數點後8位.
$table->enum(‘choices’, [‘foo’, ‘bar’]); 等同於數據庫中的 ENUM類型
$table->float(‘amount’); 等同於數據庫中的 FLOAT 類型
$table->increments(‘id’); 數據庫主鍵自增ID
$table->integer(‘votes’); 等同於數據庫中的 INTEGER 類型
$table->ipAddress(‘visitor’); 等同於數據庫中的 IP 地址
$table->json(‘options’); 等同於數據庫中的 JSON 類型
$table->jsonb(‘options’); 等同於數據庫中的 JSONB 類型
$table->longText(‘description’); 等同於數據庫中的 LONGTEXT 類型
$table->macAddress(‘device’); 等同於數據庫中的 MAC 地址
$table->mediumIncrements(‘id’); 自增ID,類型爲無符號的mediumint
$table->mediumInteger(‘numbers’); 等同於數據庫中的 MEDIUMINT類型
$table->mediumText(‘description’); 等同於數據庫中的 MEDIUMTEXT類型
$table->morphs(‘taggable’); 添加一個 INTEGER類型的 taggable_id 列和一個 STRING類型的 taggable_type列
$table->nullableTimestamps(); 和 timestamps()一樣但允許 NULL值.
$table->smallInteger(‘votes’); 等同於數據庫中的 SMALLINT 類型
$table->softDeletes(); 新增一個 deleted_at 列 用於軟刪除.
$table->string(‘email’); 等同於數據庫中的 VARCHAR 列 .
$table->string(‘name’, 100); 等同於數據庫中的 VARCHAR,帶一個長度
$table->text(‘description’); 等同於數據庫中的 TEXT 類型
$table->time(‘sunrise’); 等同於數據庫中的 TIME類型
$table->timeTz(‘sunrise’); 等同於數據庫中的 TEXT 類型
$table->time(‘sunrise’); 等同於數據庫中的 TIME類型
$table->text(‘description’); 等同於數據庫中的 TIME 類型(帶時區)
$table->tinyInteger(‘numbers’); 等同於數據庫中的 TINYINT 類型
$table->text(‘description’); 等同於數據庫中的 TEXT 類型
$table->timestamp(‘added_on’); 等同於數據庫中的 TIMESTAMP 類型
$table->timestampTz(‘added_on’);; 等同於數據庫中的 TIMESTAMP 類型(帶時區)
$table->timestampsTz(); 添加 created_at 和 updated_at列(帶時區)
$table->timestamps(); 添加 created_at 和 updated_at列
$table->unsignedBigInteger(‘votes’); 等同於數據庫中無符號的 BIGINT 類型
$table->unsignedInteger(‘votes’); 等同於數據庫中無符號的 INT 類型
$table->unsignedMediumInteger(‘votes’); 等同於數據庫中無符號的 MEDIUMINT 類型
$table->unsignedSmallInteger(‘votes’); 等同於數據庫中無符號的 SMALLINT 類型
$table->unsignedTinyInteger(‘votes’); 等同於數據庫中無符號的 TINYINT 類型
$table->uuid(‘id’); 等同於數據庫的UUID
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章