見出し画像

【Laravel 7.x】小数点の数字を登録したい

はじめに

作業工数が登録できるようにしたいということなので、小数点をDBに登録できるようにしたかった。

テーブル名:reports
工数のカラム名:man_hour

やり方

まずマイグレーションファイルを作成

php artisan make:migration add_man_hour_to_posts_table --table=reports

するとマイグレーションファイルが生成されるので、そこにこれを記載。

2020_XX_XX_XXXXXX_add_man_hour_to_posts_table.php

<省略>

public function up()
{
   Schema::table('reports', function (Blueprint $table) {
       //
       $table->double('man_hour', 2, 1)->nullable()->comment('作業工数');
   });
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
   Schema::table('reports', function (Blueprint $table) {
       //
       $table->dropColumn('man_hour');
   });
}

<省略>
$table->double('column', 15, 8);
DOUBLE equivalent with precision, 15 digits in total and 8 after the decimal point
この場合だと全部で15桁登録できて、小数点以下は8桁ということ。
php artisan migrate

<input>タグにはこのように記載

<input type="number" class="w-full form-control text-lg ml-1" name="man_hour" step="0.1">

この step="0.1" という記載がないと

画像1

そもそも登録できないので注意!

この記事が気に入ったらサポートをしてみませんか?