class Blueprint

Handles schema definitions before executing them.

Properties

protected $columns
protected array $columnModifiers
protected $dbDriver
protected $engine
protected $foreignKeys
protected $indexes
protected string|null $lastColumn
protected $table

Methods

__construct(string $table)

Constructor for Blueprint class.

after(string $column)

Specifies the position of the last defined column to appear immediately after another column in the table (MySQL only).

bigInteger(string $name)

Define a big integer column.

boolean(string $name)

Define a boolean column.

void
create()

Create the table.

void
createForeignKey(string $fk)

Create a foreign key (MySQL only).

void
createIndex(array|string $index)

Define an index on one or more columns.

date(string $name)

Define a date column.

dateTime(string $name)

Define a datetime column.

decimal(string $name, int $precision = 8, int $scale = 2)

Define a decimal column.

default(string|int|float|bool $value)

Adds a DEFAULT value to the last defined column.

void
dropColumns(array|string $columns)

Drops a column or group of columns. If a column has a restraint then warnings are presented to the user.

void
dropForeign(string $column, bool $preserveColumn = true)

Drops a foreign key constraint from the table (MySQL only).

void
dropIfExists(string $table)

Drops a table if it exists.

void
dropIndex(string $column, bool $preserveColumn = true)

Drops indexed value from the table.

void
dropPrimaryKey(string $column, bool $preserveColumn = true)

Drops primary key field from the table.

void
dropUnique(string $column, bool $preserveColumn = false)

Drops column with unique constraint from the table.

double(string $name, int $precision = 16, int $scale = 4)

Define a double column.

enum(string $name, array $values)

Define an enum column (MySQL only).

float(string $name, int $precision = 8, int $scale = 2)

Define a float column.

void
foreign(string $column, string $referencedColumn, string $onTable, string $onDelete = 'RESTRICT', string $onUpdate = 'RESTRICT')

Define a foreign key (MySQL only).

object|null
getForeignKey(string $column)

Determines if a particular column has a foreign key constraint (MySQL only).

id()

Add an ID column (primary key).

void
index(string $column)

Define an index.

bool
isForeignKey(string $column)

Tests if a column is a foreign key.

bool
isIndex(string $column)

Tests if field is an index. If true then reports to console.

bool
isPrimaryKey(string $column)

Tests if field is a primary key. If true then reports to console.

bool
isUnique(string $column)

Tests if a column has a UNIQUE constraint.

integer(string $name)

Define an integer column.

mediumInteger(string $name)

Define a medium integer column.

nullable()

Modifies last column added to the schema and make it nullable.

void
renameColumn(string $from, string $to)

Renames a particular column

void
renameForeign(string $from, string $to)

Renames a foreign key.

void
renameIndex(string $from, string $to)

Renames an indexed column by preserving and reapplying the index.

void
renamePrimaryKey(string $from, string $to)

Renames the table's primary key.

void
renameUnique(string $from, string $to)

Renames a column with a unique constraint by preserving and reapplying the index.

void
setForeignKeys()

Sets foreign keys during creating of table or renaming of existing foreign key.

void
setUnique(string $column)

Sets the unique index on a column.

smallInteger(string $name)

Define a small integer column.

softDeletes()

Define a soft delete column.

string(string $name, int $length = 255)

Define a string column.

text(string $name)

Define a text column.

time(string $name)

Define a time column.

timestamp(string $name)

Define a timestamp column.

void
timestamps()

Define timestamps (created_at and updated_at).

tinyInteger(string $name)

Define a tiny integer column.

unique()

Adds a unique index to the last defined column.

unsignedInteger(string $name)

Define an unsigned integer column (MySQL only).

unsignedBigInteger(string $name)

Define an unsigned big integer column (MySQL only).

void
update()

Update an existing table.

uuid(string $name)

Define a UUID column (MySQL only).

Details

at line 28
__construct(string $table)

Constructor for Blueprint class.

Parameters

string $table

The name of the table to be modified.

at line 48
Blueprint after(string $column)

Specifies the position of the last defined column to appear immediately after another column in the table (MySQL only).

This method is only meaningful during ALTER TABLE operations (i.e., when calling $table->update()). The generated SQL will include an AFTER column_name clause to control column order.

Example: $table->string('nickname')->after('last_name'); // Produces: ALTER TABLE users ADD COLUMN nickname VARCHAR(255) AFTER last_name;

Parameters

string $column

The existing column name after which the new column should be added.

Return Value

Blueprint

Returns the current Blueprint instance for method chaining.

at line 61
Blueprint bigInteger(string $name)

Define a big integer column.

Parameters

string $name

The name of the column to be created as BIGINT.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 73
Blueprint boolean(string $name)

Define a boolean column.

Parameters

string $name

The name of the column to be created as TINYINT(1).

Return Value

Blueprint

Return the instance to allow method chaining.

at line 82
void create()

Create the table.

Return Value

void

at line 109
protected void createForeignKey(string $fk)

Create a foreign key (MySQL only).

Parameters

string $fk

The full SQL statement to create the foreign key constraint. This should be a valid ALTER TABLE query for adding a foreign key. Example: "ALTER TABLE posts ADD FOREIGN KEY (user_id) REFERENCES users(id)"

Return Value

void

at line 130
protected void createIndex(array|string $index)

Define an index on one or more columns.

This method registers a standard (non-unique) index to be created after the table is created. The index will be applied to the given column or set of columns. The index name is automatically generated as {table}_{column}_idx for a single column or based on the provided structure.

Note: Actual SQL index creation occurs in the create() method via createIndex().

Parameters

array|string $index

Return Value

void

at line 157
Blueprint date(string $name)

Define a date column.

Parameters

string $name

The name of the column to be created as DATE.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 169
Blueprint dateTime(string $name)

Define a datetime column.

Parameters

string $name

The name of the column to be created as DATETIME.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 183
Blueprint decimal(string $name, int $precision = 8, int $scale = 2)

Define a decimal column.

Parameters

string $name

The name of the column.

int $precision

Total number of digits.

int $scale

Number of digits after the decimal.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 203
Blueprint default(string|int|float|bool $value)

Adds a DEFAULT value to the last defined column.

This method appends a default value to the most recently added column in the schema definition. It supports string, integer, float, and boolean values. If no columns have been added yet, it throws an exception. In SQLite, default values for certain column types like TEXT and BLOB are skipped.

Parameters

string|int|float|bool $value

The default value to assign to the last defined column. Strings will be wrapped in quotes. Other types will be cast directly.

Return Value

Blueprint

Returns the current Blueprint instance for method chaining.

Exceptions

Exception

at line 234
void dropColumns(array|string $columns)

Drops a column or group of columns. If a column has a restraint then warnings are presented to the user.

Parameters

array|string $columns

An individual column or an array of columns to drop.

Return Value

void

at line 290
void dropForeign(string $column, bool $preserveColumn = true)

Drops a foreign key constraint from the table (MySQL only).

Parameters

string $column

The name of the column to be dropped.

bool $preserveColumn

When true only the foreign key constraint is removed. If set to false the column is also dropped from the table. The default value is true.

Return Value

void

at line 325
void dropIfExists(string $table)

Drops a table if it exists.

Parameters

string $table

The name of the table to drop if it exists.

Return Value

void

at line 340
void dropIndex(string $column, bool $preserveColumn = true)

Drops indexed value from the table.

Parameters

string $column

The name of the column to be dropped.

bool $preserveColumn

When true only the index constraint is removed. If set to false the column is also dropped from the table. The default value is true.

Return Value

void

at line 373
void dropPrimaryKey(string $column, bool $preserveColumn = true)

Drops primary key field from the table.

Parameters

string $column

The name of the column to be dropped.

bool $preserveColumn

When true only the primary key constraint is removed. If set to false the column is also dropped from the table. The default value is true.

Return Value

void

at line 405
void dropUnique(string $column, bool $preserveColumn = false)

Drops column with unique constraint from the table.

Parameters

string $column

The name of the column to be dropped.

bool $preserveColumn

When true only the unique constraint is removed. If set to false the column is also dropped from the table. The default value is true.

Return Value

void

at line 476
Blueprint double(string $name, int $precision = 16, int $scale = 4)

Define a double column.

Parameters

string $name

The name of the column to be created as DOUBLE.

int $precision

Total number of digits.

int $scale

Number of digits after the decimal.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 489
Blueprint enum(string $name, array $values)

Define an enum column (MySQL only).

Parameters

string $name

The name of the enum column.

array $values

An array of allowed values for the ENUM.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 508
Blueprint float(string $name, int $precision = 8, int $scale = 2)

Define a float column.

Parameters

string $name

The name of the column to be created as FLOAT.

int $precision

Total number of digits.

int $scale

Number of digits after the decimal.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 523
void foreign(string $column, string $referencedColumn, string $onTable, string $onDelete = 'RESTRICT', string $onUpdate = 'RESTRICT')

Define a foreign key (MySQL only).

Parameters

string $column

The column name to add a foreign key constraint on.

string $referencedColumn
string $onTable

The name of the table being referenced.

string $onDelete

Action on delete (e.g., CASCADE).

string $onUpdate

Action on update (e.g., CASCADE).

Return Value

void

at line 545
private object|null getForeignKey(string $column)

Determines if a particular column has a foreign key constraint (MySQL only).

Parameters

string $column

The name of the column.

Return Value

object|null

The results returned from the database.

at line 572
id()

Add an ID column (primary key).

at line 582
void index(string $column)

Define an index.

Parameters

string $column

The column name to add an index on.

Return Value

void

at line 594
private bool isForeignKey(string $column)

Tests if a column is a foreign key.

If true, reports to the console. Helps prevent unsafe renames or drops.

Parameters

string $column

The name of the column to check.

Return Value

bool

True if the column has a foreign key constraint.

at line 638
private bool isIndex(string $column)

Tests if field is an index. If true then reports to console.

Parameters

string $column

The name of the field we want to test.

Return Value

bool

True if value is an index and otherwise we return false.

at line 667
private bool isPrimaryKey(string $column)

Tests if field is a primary key. If true then reports to console.

Parameters

string $column

The name of the field we want to test.

Return Value

bool

True if value is a primary key and otherwise we return false.

at line 699
private bool isUnique(string $column)

Tests if a column has a UNIQUE constraint.

If true, reports to the console. This helps avoid modifying unique-indexed columns unintentionally during schema changes like dropping or renaming.

Parameters

string $column

The name of the column to check.

Return Value

bool

True if the column is unique, false otherwise.

at line 743
Blueprint integer(string $name)

Define an integer column.

Parameters

string $name

The name of the column to be created as INT.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 756
Blueprint mediumInteger(string $name)

Define a medium integer column.

Parameters

string $name

The name of the column to be created as MEDIUMINT.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 767
Blueprint nullable()

Modifies last column added to the schema and make it nullable.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 782
void renameColumn(string $from, string $to)

Renames a particular column

Parameters

string $from

The column's original name.

string $to

The column's new name.

Return Value

void

at line 807
void renameForeign(string $from, string $to)

Renames a foreign key.

Parameters

string $from

The original column name.

string $to

The new column name.

Return Value

void

at line 849
void renameIndex(string $from, string $to)

Renames an indexed column by preserving and reapplying the index.

Parameters

string $from

The original column name.

string $to

The new column name.

Return Value

void

at line 884
void renamePrimaryKey(string $from, string $to)

Renames the table's primary key.

Parameters

string $from

The original column name.

string $to

The new column name.

Return Value

void

at line 919
void renameUnique(string $from, string $to)

Renames a column with a unique constraint by preserving and reapplying the index.

Parameters

string $from

The original column name.

string $to

The new column name.

Return Value

void

at line 953
private void setForeignKeys()

Sets foreign keys during creating of table or renaming of existing foreign key.

Return Value

void

at line 970
private void setUnique(string $column)

Sets the unique index on a column.

Parameters

string $column

The column where the unique index constraint will be applied.

Return Value

void

at line 985
Blueprint smallInteger(string $name)

Define a small integer column.

Parameters

string $name

The name of the column to be created as SMALLINT.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 996
Blueprint softDeletes()

Define a soft delete column.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1008
Blueprint string(string $name, int $length = 255)

Define a string column.

Parameters

string $name

The name of the column.

int $length

The maximum length of the string column.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1021
Blueprint text(string $name)

Define a text column.

Parameters

string $name

The name of the column to be created as TEXT.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1033
Blueprint time(string $name)

Define a time column.

Parameters

string $name

The name of the column to be created as TIME.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1045
Blueprint timestamp(string $name)

Define a timestamp column.

Parameters

string $name

The name of the column to be created as TIMESTAMP.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1054
void timestamps()

Define timestamps (created_at and updated_at).

Return Value

void

at line 1065
Blueprint tinyInteger(string $name)

Define a tiny integer column.

Parameters

string $name

The name of the column to be created as TINYINT or INTEGER depending on DB driver.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1078
Blueprint unique()

Adds a unique index to the last defined column.

Return Value

Blueprint

Exceptions

Exception

at line 1094
Blueprint unsignedInteger(string $name)

Define an unsigned integer column (MySQL only).

Parameters

string $name

The name of the column to be created as unsigned INT (MySQL) or INTEGER.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1110
Blueprint unsignedBigInteger(string $name)

Define an unsigned big integer column (MySQL only).

Parameters

string $name

The name of the column to be created as unsigned BIGINT (MySQL) or INTEGER for SQLite.

Return Value

Blueprint

Return the instance to allow method chaining.

at line 1124
void update()

Update an existing table.

Return Value

void

at line 1157
uuid(string $name)

Define a UUID column (MySQL only).

Parameters

string $name

The name of the column to be created as UUID.