溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Laravel中如何使用Migrations

發(fā)布時(shí)間:2021-09-05 08:42:35 來源:億速云 閱讀:148 作者:小新 欄目:編程語言

這篇文章主要介紹Laravel中如何使用Migrations,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

Laravel:使用Migrations

1、首先利用artisan創(chuàng)建一個(gè)可遷移的數(shù)據(jù)表模板,該命令運(yùn)行后會(huì)在database/migrations目錄下生成一個(gè)文件

php artisan make:migration create_fees_count_table --create=fees_count

2、生成的文件包含up和down兩個(gè)方法,其中up中是包含了添加表,添加列,添加索引等等一切的描述,down比較簡單,就是刪除表,當(dāng)然里面還可以有一些其他邏輯

3、up中支持的數(shù)據(jù)表列類型,做個(gè)備注,暫時(shí)不做翻譯

CommandDescription
$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.
$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');MEDIUMTEXT equivalent for the database.
$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();Adds deleted_at column for soft deletes.
$table->string('email');VARCHAR equivalent column.
$table->string('name', 100);VARCHAR equivalent with a length.
$table->text('description');TEXT equivalent for the database.
$table->time('sunrise');TIME equivalent for the database.
$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.
$table->uuid('id');UUID equivalent for the database.

4、表創(chuàng)建完成后,直接執(zhí)行,由于我之前很多表創(chuàng)建并沒有使用php artisan  migrate,所以直接運(yùn)行該命令導(dǎo)致提示部分表存在,所以,我把這個(gè)文件轉(zhuǎn)移到database下tmp目錄下,命令上添加 --path 'database/tmp',再次運(yùn)行成功

php artisan  migrate

5、數(shù)據(jù)庫中觀察即發(fā)現(xiàn)表已創(chuàng)建!

以上是“Laravel中如何使用Migrations”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI