Laravel - handling columns and table in migration

Laravel - handling columns and table in migration

2023-07-28
2 min read

Introduction

You can simply make migration file with following command.

bash
php artisan make:migration write_your_message.

Just type any message instead of write_your_message. Conventionally, Underscore is used for spaces in message.

In addition, you can find available types of column via the link.

Create table

php
Schema::create('customers', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamps();
});

The craete() method on Schema facade will create table named by first argument. You can define columns in function passed as a second parameter.

Update table

php
Schema::table('customers', function (Blueprint $table) {
    $table->string('email');
});

The table() method on the Schema facade is used to update existing tables. If there is no table name by first argument, error will be occurred.

The hasTable() method allows to check existence of a table.

php
Schema::hasTable('customers');

If table exists, it will return true. Otherwise, false.

Rename table

php
Schema::rename('customers', 'newCustomers');

You can change name of table with rename() method. Before you rename the table, you should verify table which has an explicit name in your migration files instead of letting Laravel assign a convention based name for foreign key.

Delete (Drop) table

php
Schema::drop('customers');

The drop method drop the table named by argument, but there could be error if there is no existing table by name.

Soft drop

php
Schema::dropIfExists('customers');

If you want to drop table only if table is existed, you can use dropIfExists() method in Schema. It's a safer way than using drop() method.

add columns to existing table

php
Schema::table('customers', function (Blueprint $table) {
    $table->string('email');
    $table->integer('age')->nullable();
});

If you want to add more columns to exiting table, add columns with table() method on Schema.

You may check for existence of a column using hasColumn() method.

php
Schema::hasColumn('users', 'email')

Update columns

php
Schema::table('customers', function (Blueprint $table) {
    $table->string('email', 100)->nullable()->change();
});

change() method allows you to modify the type and attributes of exiting columns. Call change() method at the last after you write new types or attributes for column.

Update names

php
Schema::table('customer', function (Blueprint $table) {
    $table->renameColumn('name', 'firstName');
});

The renameColumn() method changes the name of column. First argument is for current name and second argument is for name that you want to change.

Delete (Drop) columns

php
Schema::table('customers', function (Blueprint $table) {
    $table->dropColumn('email');
});
The `dropColumn()` method allows to delete columns.

After you write dropColumn() in up function, you should add column back in down() function. Look at the following example

php
Schema::table('customers', function (Blueprint $table) {
    $table->string('email');
});

Ref

Free open source project made by Youngjin