How to Add New Column to Existing Table in Laravel

Laravel comes with a built-in feature called Migrations for managing database. Migrations are nothing but a bunch of PHP files that are used to create or modify your database.

Useful Read: Helpers File in Laravel 5

In this tutorial, we are going to use migrations to add a new column to existing database in Laravel. In this example we are going to add a address column to users table.

First we need to create a new migration with artisan command.

php artisan make:migration add_address_column_to_users

Then, inside up method we are going use Schema Builder to add a new column.

public function up()
{
    Schema::table('users', function($table)
    {
        $table->string('address')->after('name');
    });
}

after is an optional feature which will allow you to add a new column after a specific column. If you are not using after your column will appear at the bottom of all columns.

0 Comments

Leave a Comment.