首頁 >php框架 >Laravel >五步驟搞定Laravel Migrations的使用

五步驟搞定Laravel Migrations的使用

藏色散人
藏色散人轉載
2021-09-01 16:10:592562瀏覽

本文由Laravel教學專欄給大家介紹,主要內容是“Laravel如何使用Migrations”,希望對需要的朋友有所幫助!

Laravel:使用Migrations

1、先利用artisan建立一個可遷移的資料表模板,該指令執行後會在database/migrations目錄下產生一個檔案

php artisan make:migration create_fees_count_table --create=fees_count

2、產生的檔案包含up和down兩個方法,其中up​​中是包含了新增表,新增列,加入索引等等一切的描述,down比較簡單,就是刪除表,當然裡面還可以有一些其他邏輯

3、up中支援的資料表列類型,做個備註,暫時不做翻譯

##$table->double('column', 15, 8);DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point.$table->enum('choices', ['foo', 'bar'] );ENUM equivalent for the database.$table->float('amount');FLOAT equivalent for the database. $table->increments('id');#Incrementing ID (primary key) using a "UNSIGNED INTEGER" equivalent.#$table->integer('votes');INTEGER equivalent for the database.$table->json(' options');JSON equivalent for the database.$table->jsonb('options');JSONB equivalent for the database.$table->longText('description');LONGTEXT equivalent for the database.# $table->mediumInteger('numbers');MEDIUMINT equivalent for the database.##$table->mediumText('description');$table->morphs('taggable');#Adds INTEGER taggable_id and STRING taggable_type.$table->nullableTimestamps();Same as timestamps(), except allows NULLs.$table-> ;rememberToken();Adds remember_token as VARCHAR(100) NULL.$table->smallInteger('votes'); SMALLINT equivalent for the database.##$table->softDeletes();$table->string('email');$table->string('name', 100); $table->text('description');$table->time('sunrise');##$table-> ;tinyInteger('numbers');TINYINT equivalent for the database.$table->timestamp('added_on');# TIMESTAMP equivalent for the database.$table->timestamps();Adds created_at and updated_at columns.
Command Description
$table->bigIncrements('id'); #Incrementing ID (primary key) using a "UNSIGNED BIG INTEGER" equivalent.
$table->bigInteger('votes'); BIGINT equivalent for the database.
$table->binary('data'); BLOB equivalent for the database.
$table->boolean(' confirmed'); BOOLEAN equivalent for the database.
$table->char('name', 4); CHAR equivalent with a length.
$table->date('created_at'); DATE equivalent for the database.
#$table->dateTime('created_at'); DATETIME equivalent for the database.
$table->decimal('amount', 5 , 2); DECIMAL equivalent with a precision and scale.
MEDIUMTEXT equivalent for the database.
Adds INTEGER taggable_id and STRING taggable_type.
Adds deleted_at column for soft deletes.
VARCHAR equivalent column.
VARCHAR equivalent with a length.
TEXT equivalent for the database.
TIME equivalent for the database.

############## #$table->uuid('id');######UUID equivalent for the database.###############4、表格建立完成後,直接執行,由於我之前很多表創建並沒有使用php artisan  migrate,所以直接運行該命令導致提示部分錶存在,所以,我把這個文件轉移到database下tmp目錄下,命令上添加--path 'database/tmp',再次運行成功###
php artisan  migrate
###5、資料庫中觀察即發現表已建立! ############相關推薦:###最新的五個Laravel影片教學#########

以上是五步驟搞定Laravel Migrations的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除