我有一個現有的專案,現在我必須在所有現有的基礎上添加一個新的欄目,我不知道如何才能在Laravel遷移中實作這一點
CodePudding
uj5u.com熱心網友回復:
php artisan make:migration add_columnname_to_tablename_table --table=tablename
在游覽檔案中
public function up()
{
Schema::table('tablename', function (Blueprint $table) {
$table->datatype('newcolumnname')->nullable()。
});
}
public function down()
{
Schema::table('tablame', function($table) {
$table->dropColumn('newcolumnname'/span>)。
});
}
運行
php artisan migrate
uj5u.com熱心網友回復:
假設你想在表tablename上添加列newcolumnname。
首先創建遷移檔案:
php artisan make:migration add_newcolumnname_to_tablename --table="tablame"
然后在生成的遷移檔案的up函式中:
public function up()
{
Schema::table('tablame', function($table) {
$table->text('newcolumnname'); //suppose the datatype is text.
});
如果你想對多個表進行這樣的操作,對于artisan命令,你可以:
php artisan make:migration add_singlecolumn_to_tables
對于向上功能,你可以:
public function up()。
{
$tables=['table1','table2','table3']; /DB: :select('SHOW TABLES');
for($i=0; $i<count($tables);$i ){
Schema:: table($tables[$i], function (Blueprint $table) {
$table->text('test_column')->nullable(); //suppose the datatype is text.
});
}
uj5u.com熱心網友回復:
只需使用:
php artisan make:migration new_column_table
at database/migrations/[datetime]_new_column_table.php
public function up() /span>{
Schema::table('table_a', function(Blueprint $table) {
$table->text('new_column')->nullable()。
});
Schema::table('table_a', function(Blueprint $table) {
$table->text('new_column')->nullable()。
});
Schema::table('table_c', function(Blueprint $table) {
$table->text('new_column')->nullable()。
});
}
public function down()
{
Schema::table('table_a', function (Blueprint$table) {
$table->dropColumn('new_column'/span>)。
});
Schema::table('table_b', function (Blueprint$table) {
$table->dropColumn('new_column'/span>)。
});
Schema::table('table_c', function (Blueprint$table) {
$table->dropColumn('new_column'/span>)。
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/306790.html
標籤:
上一篇:Xcode視圖自動布局問題
