在laravel模式中为价格创建列

时间:2017-07-08 10:11:05

标签: php schema laravel-5.4

我想在laravel架构中创建价格列。

public function up()
{
    Schema::create('cameras', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('condition');
        $table->string('price');
        $table->string('type');
        $table->timestamps();
    });
}

或者我可以使用替代数据类型吗?因为我在文档中没有看到任何货币价值。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:5)

除非您不想保存货币,否则有“浮动”类型可供您使用
即: $ table-> float('amount')
可用类型列在文档中。 https://laravel.com/docs/5.4/migrations#creating-columns

如果您确实希望使用价格保存货币,则必须使用字符串,或者为货币类型创建新列可能是另一种解决方法。

答案 1 :(得分:0)

我同意@ stardust4891,最好使用小数来精确存储价格值:

$this->decimal('price', 10, 2);

或者,您也可以使用整数并将金额存储在基本货币单位(例如美分)中,然后乘以100。