我尝试在Laravel 5.3项目中遵循3个代码,将新的十进制列添加到现有表中.但它每次都会给出同样的错误.
Schema::table('mileages', function (Blueprint $table) {
$table->addColumn('decimal', 'cost');
});
Run Code Online (Sandbox Code Playgroud)
和
Schema::table('mileages', function (Blueprint $table) {
$table->addColumn('decimal', 'cost', ['default'=>0]);
});
Run Code Online (Sandbox Code Playgroud)
和
Schema::table('mileages', function (Blueprint $table) {
$table->addColumn('decimal', 'cost', ['default'=>'0,0']);
});
Run Code Online (Sandbox Code Playgroud)
错误是:
[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)
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
您正在尝试使用->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
});
Run Code Online (Sandbox Code Playgroud)
这是为了下来:
Schema::table('mileages', function (Blueprint $table) {
$table->dropColumn('cost');
});
Run Code Online (Sandbox Code Playgroud)
这是来自这里的文档,虽然它们没有明确说明.