Поле float
Метод float используется в миграциях Laravel для создания колонки с числами с плавающей точкой. Первым параметром передаётся имя колонки, вторым и третьим - общее количество цифр и количество цифр после запятой соответственно. По умолчанию колонка создаётся с типом DOUBLE в большинстве СУБД.
Синтаксис
<?php
Schema::table('table_name', function (Blueprint $table) {
$table->float($column, $total, $places);
});
?>
Пример
Создадим таблицу товаров с полем цены типа float:
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->float('price', 10, 2);
$table->integer('quantity');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
};
?>
Пример
Создадим колонку с рейтингом без указания точности:
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->float('rating')->nullable();
});
}
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('rating');
});
}
};
?>
Пример
Добавим поле веса с тремя знаками после запятой:
<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
return new class extends Migration
{
public function up()
{
Schema::table('products', function (Blueprint $table) {
$table->float('weight', 8, 3)->default(0);
});
}
public function down()
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('weight');
});
}
};
?>