Add new columns in a table laravel

Add new columns in a table laravel

Here I will show you how you can add news columns in already created table users. Needs alter table users to add a new column in users table. In laravel table method on the Schema facade is used to create, update or delete existing columns or tables. To create a migration, you may use the migrate:make command. The name of the migration must be different. With migrations in laravel, we can create, modify database tables without using phpmyadmin. In Laravel migrations are stores in project_folder/database/migrations in proper order. Every migration file is associated with the table. We can create migration using php artisan command and the name of migration is combination of time and date when migration command executed (i.e. 2020_06_09_000000_create_users_table). We can create a table with its columns using this file. We can also add columns in any already created table. Migration structure consists of 2 functions: up and down. Up function is used to add, insert, modify table or columns and Down function is used to delete columns from a table or drop a table.

To assume I've a table task which already exists in my database with column names id, tast_title, task_description, task_status, created_at and updated_at. Now i want to add a new column image in tasks table.

Follow below steps to add a new column in the already created table.

Run the below command to create a new migration.

php artisan migrate:make add_columns_to_users_table

You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:

public function up()
{
    Schema::table('tasks', function($table) {
        $table->string('image');
    });
}

public function down()
{
    Schema::table('tasks', function($table) {
        $table->dropColumn('image');
    });
}

after that run your migrations:

php artisan migrate

You can add more columns based on your requirements on any type. Column image will add at the end of the table, but if you do not want to add it at the end than you can used after() modifier in laravel.

public function up()
{

    Schema::table('tasks', function($table) {
        $table->string('image')->after('id'); // it will add column after id
});
}

There are some more modifiers which are very useful in laravel. Let me explain you some of the modifiers with an example.

first(): It will add a column at first position in the table.

public function up()
{

    Schema::table('tasks', function($table) {
        $table->string('image')->first();
    });
}

default($val): Using this modifier, we can set default value of any column.

public function up()
{

    Schema::table('tasks', function($table) {
        $table->string('image')->default('dummy.jpg');
    });
}

nullable(): Some fields are optional in the database, on those columns we use nullable modifier.

public function up()
{

    Schema::table('tasks', function($table) {
        $table->string('image')->nullable();
    });
}

And if you want to make a column nullable which is already created in a table, then you have to create a new migration and add below code in migration file up function.

public function up()
{

    Schema::table('tasks', function($table) {
        $table->string('image')->nullable()->change();
    });
}

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Jhosaf
Jhosaf
20 Dec 2020

Thanks to author, for explaining with the help of sch an easy examples

Add your comment