2017-02-14 43 views
2

我在Laravel 5.3項目中嘗試了以下3個代碼以將新的小數列添加到現有表中。但每次都會出現相同的錯誤。如何將小數點列添加到Laravel 5.3遷移中的現有表中

Schema::table('mileages', function (Blueprint $table) { 
    $table->addColumn('decimal', 'cost'); 
}); 

Schema::table('mileages', function (Blueprint $table) { 
    $table->addColumn('decimal', 'cost', ['default'=>0]); 
}); 

Schema::table('mileages', function (Blueprint $table) { 
    $table->addColumn('decimal', 'cost', ['default'=>'0,0']); 
}); 

錯誤是:

[Illuminate\Database\QueryException]                     
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that 
corresponds to your MariaDB server version for the right syntax to use near ') not null' at line 1 (SQL: alter table `mileages` add `cost` decimal (,) not null) 

我這麼想嗎?

回答

2

您正在嘗試使用->addColumn()這不是正確的語法。

創建一個新的遷移,php artisan make:migrate add_cost_column_to_mileages

然後遷移裏面,你希望它看起來像這樣了:

Schema::table('mileages', function (Blueprint $table) { 
    $table->decimal('cost', 5,2); //Substitute 5,2 for your desired precision 
}); 

這爲下:

Schema::table('mileages', function (Blueprint $table) { 
    $table->dropColumn('cost'); 
}); 

這是從文檔here,雖然他們不明確清楚。

+0

是的,它的工作。謝謝!。還有'$ table-> addColumn('decimal','cost',['default'=> 0,'total'=> 8,'places'=> 2]);'正在工作。 – Eranda

相關問題