Поле bigInteger
Метод bigInteger используется в миграциях Laravel для определения колонки с типом данных BIGINT. Этот тип предназначен для хранения целых чисел большого диапазона, от -9 223 372 036 854 775 808 до 9 223 372 036 854 775 807 для знаковых или от 0 до 18 446 744 073 709 551 615 для беззнаковых. Метод принимает имя колонки в качестве первого параметра, а вторым необязательным параметром можно передать длину для некоторых СУБД.
Синтаксис
<?php
Schema::create('table_name', function (Blueprint $table) {
$table->bigInteger('column_name');
});
?>
Пример
Давайте создадим миграцию для таблицы 'orders' с полем 'order_number' типа BIGINT:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->bigInteger('order_number');
$table->integer('amount');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('orders');
}
};
?>
Результат выполнения миграции:
"Migration table created successfully"
"Migrating: 2026_01_01_000000_create_orders_table"
"Migrated: 2026_01_01_000000_create_orders_table"
Пример
Создадим беззнаковое поле BIGINT с помощью метода unsignedBigInteger:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('external_id');
$table->string('name');
$table->decimal('price', 10, 2);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('products');
}
};
?>
Пример
Используем поле BIGINT для внешнего ключа, связывающего таблицы:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('order_items', function (Blueprint $table) {
$table->id();
$table->bigInteger('order_id');
$table->foreign('order_id')->references('id')->on('orders');
$table->string('product_name');
$table->integer('quantity');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('order_items');
}
};
?>
Пример
Создадим поле BIGINT с дополнительными модификаторами, такими как значение по умолчанию и комментарий:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->bigInteger('transaction_id')->default(0)->comment('Идентификатор транзакции');
$table->decimal('amount', 15, 2);
$table->string('status');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('transactions');
}
};
?>
Смотрите также
-
метод
unsignedBigInteger,
который создает беззнаковое целое число -
метод
integer,
который создает целочисленную колонку -
метод
id,
который создает первичный ключ -
метод
foreignId,
который создает внешний ключ