Laravel: Migrations (3)

Tram Ho

4. Column

a. Creating Columns

The methods on the Schema facade can be used to update existing tables. Like the create method, the table method accepts two arguments: the table name and the Closure represent the Blueprint that we can use to add columns to the table:

Available Column Types

The schema builder has many column types that we can specify when building tables:

Commanddescription
$table->bigIncrements('id');Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.
$table->bigInteger('votes');BIGINT equivalent column.
$table->binary('data');BLOB equivalent column.
$table->boolean('confirmed');BOOLEAN equivalent column.
$table->char('name', 100);CHAR equivalent column with an optional length.
$table->date('created_at');DATE equivalent column.
$table->dateTime('created_at');DATETIME equivalent column.
$table->dateTimeTz('created_at');DATETIME (with timezone) equivalent column.
$table->decimal('amount', 8, 2);DECIMAL equivalent column with a precision (total digits) and scale (decimal digits).
$table->double('amount', 8, 2);DOUBLE equivalent column with a precision (total digits) and scale (decimal digits).
$table->enum('level', ['easy', 'hard']);ENUM equivalent column.
$table->float('amount', 8, 2);FLOAT equivalent column with a precision (total digits) and scale (decimal digits).
$table->geometry('positions');GEOMETRY equivalent column.
$table->geometryCollection('positions');GEOMETRYCOLLECTION equivalent column.
$table->increments('id');Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.
$table->integer('votes');INTEGER equivalent column.
$table->ipAddress('visitor');IP address equivalent column.
$table->json('options');JSON equivalent column.
$table->jsonb('options');JSONB equivalent column.
$table->lineString('positions');LINESTRING equivalent column.
$table->longText('description');LONGTEXT equivalent column.
$table->macAddress('device');MAC address equivalent column.
$table->mediumIncrements('id');Auto-incrementing UNSIGNED MEDIUMINT (primary key) equivalent column.
$table->mediumInteger('votes');MEDIUMINT equivalent column.
$table->mediumText('description');MEDIUMTEXT equivalent column.
$table->morphs('taggable');Adds taggable_id UNSIGNED BIGINT and taggable_type VARCHAR equivalent columns.
$table->uuidMorphs('taggable');Adds taggable_id CHAR (36) and taggable_type VARCHAR (255) UUID equivalent columns.
$table->multiLineString('positions');MULTILINESTRING equivalent column.
$table->multiPoint('positions');MULTIPOINT equivalent column.
$table->multiPolygon('positions');MULTIPOLYGON equivalent column.
$table->nullableMorphs('taggable');Adds nullable versions of morphs () columns.
$table->nullableUuidMorphs('taggable');Adds nullable versions of uuidMorphs () columns.
$table->nullableTimestamps();Alias ​​of timestamps () method.
$table->point('position');POINT equivalent column.
$table->polygon('positions');POLYGON equivalent column.
$table->rememberToken();Adds a nullable remember_token VARCHAR (100) equivalent column.
$table->set('flavors', ['strawberry', 'vanilla']);SET equivalent column.
$table->smallIncrements('id');Auto-incrementing UNSIGNED SMALLINT (primary key) equivalent column.
$table->smallInteger('votes');SMALLINT equivalent column.
$table->softDeletes();Adds a nullable deleted_at TIMESTAMP equivalent column for soft deletes.
$table->softDeletesTz();Adds a nullable deleted_at TIMESTAMP (with timezone) equivalent column for soft deletes.
$table->string('name', 100);VARCHAR equivalent column with an optional length.
$table->text('description');TEXT equivalent column.
$table->time('sunrise');TIME equivalent column.
$table->timeTz('sunrise');TIME (with timezone) equivalent column.
$table->timestamp('added_on');TIMESTAMP equivalent column.
$table->timestampTz('added_on');TIMESTAMP (with timezone) equivalent column.
$table->timestamps();Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
$table->timestampsTz();Adds nullable created_at and updated_at TIMESTAMP (with timezone) equivalent columns.
$table->tinyIncrements('id');Auto-incrementing UNSIGNED TINYINT (primary key) equivalent column.
$table->tinyInteger('votes');TINYINT equivalent column.
$table->unsignedBigInteger('votes');UNSIGNED BIGINT equivalent column.
$table->unsignedDecimal('amount', 8, 2);UNSIGNED DECIMAL equivalent column with a precision (total digits) and scale (decimal digits).
$table->unsignedInteger('votes');UNSIGNED INTEGER equivalent column.
$table->unsignedMediumInteger('votes');UNSIGNED MEDIUMINT equivalent column.
$table->unsignedSmallInteger('votes');UNSIGNED SMALLINT equivalent column.
$table->unsignedTinyInteger('votes');UNSIGNED TINYINT equivalent column.
$table->uuid('id');UUID equivalent column.
$table->year('birth_year');YEAR equivalent column.

b. Column Modifiers

In addition to the column types listed above, there are a number of “column modifiers” that you can use while adding a column to the database table. For example, to make the column “nullable”, you could use the nullable method:

Below is a list of all available column modifiers. This list does not include index modifiers:

Column 1Column 2
->after('column')Place the column “after” another column (MySQL)
->autoIncrement()Set INTEGER columns as auto-increment (primary key)
->charset('utf8')Specify a character set for the column (MySQL)
->collation('utf8_unicode_ci')Specify a collation for the column (MySQL / PostgreSQL / SQL Server)
->comment('my comment')Add a comment to a column (MySQL / PostgreSQL)
->default($value)Specify a “default” value for the column
->first()Place the column “first” in the table (MySQL)
->nullable($value = true)Allows (by default) NULL values ​​to be inserted into the column
->storedAs($expression)Create a stored generated column (MySQL)
->unsigned()Set INTEGER columns as UNSIGNED (MySQL)
->useCurrent()Set TIMESTAMP columns to use CURRENT_TIMESTAMP as default value
->virtualAs($expression)Create an identity column with specified sequence options (PostgreSQL)
->generatedAs($expression)Create an identity column with specified sequence options (PostgreSQL)
-> always ()Defines the precedence of sequence values ​​over input for an identity column (PostgreSQL)

c. Modifying Columns

Prerequisites

Before modifying a column, make sure to add dependency doctrine / dbal to the composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to that column:

Updating Column Attributes

The change method allows you to modify some of the existing column types into new ones or modify the column properties. For example, you might want to increase the lenght of a string column from 25 to 50:

We can modify the column not null to allow null by the following:

Renaming Columns

To rename a column, you can use the renameColumn method on the Schema generator. Before renaming a column, be sure to add the doctrine / dbal dependency to your composer.json file:

d. Dropping Columns

To delete a column, use the dropColumn method on the Schema generator.

You can delete multiple columns from a table by passing an array of column names to the dropColumn method:

Available Command Aliases

Commanddescription
$table->dropMorphs('morphable');Drop the morphable_id and morphable_type columns.
$table->dropRememberToken();Drop the remember_token column.
$table->dropSoftDeletes();Drop the deleted_at column.
$table->dropSoftDeletesTz();Alias ​​of dropSoftDeletes () method.
$table->dropTimestamps();Drop the created_at and updated_at columns.
$table->dropTimestampsTz();Alias ​​of dropTimestamps () method.

Source: link .

Share the news now

Source : Viblo