php - 如何在 Laravel 5.3 迁移中将小数列添加到现有表

标签 php laravel-5.3

我尝试在 Laravel 5.3 项目中按照 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) 

我错过了什么吗?

最佳答案

您正在尝试使用 ->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 ,尽管他们没有明确说明。

关于php - 如何在 Laravel 5.3 迁移中将小数列添加到现有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42222583/

相关文章:

php - 从 PHP 和 MySQL 中的另一个表中排除项目

javascript - 使用填充的下拉列表复制 php 表的新行

php - 如何在where条件下传递数组?拉维尔 5.3

mysql - 连接存在引用的表

eloquent - Laravel/Eloquent whereIn 为 null

Laravel 5.3 查询从 4 个通过外键连接的表中获取结果

laravel - 在 Laravel 5.2 中工作的查询在 Laravel 5.3 中给我错误

php - 显示从 Laravel 数据库中选中的复选框

php - MySQL 只返回查询结果中的{第一行}!

php - 我该如何修复我的程序?