How To Modify The Columns In Existing Table With Laravel

admin_img Posted By Bajarangi soft , Posted On 03-09-2020

Modifying columns in laravel made is easy when you created migration file and added new columns and later you need to change columns name or datatype to table can be done in laravel. so in this articles we are going to discuss how can we modify the column name or datataype.

How To Modify The Columns In Existing Table With Laravel

Modifying Columns
 

Prerequisites

Before modifying a column, be sure to add the doctrine/dbal dependency to your 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 required adjustments:
 

composer require doctrine/dbal


Updating Column Attributes The change method allows you to modify type and attributes of existing columns. For example, you may wish to increase the size of a string column. To see the change method in action, let's increase the size of the name column from 25 to 50:
 

public function up()
{
    Schema::table('test', function (Blueprint $table) {
        $table->increments('id');
        $table->string('test_name')->change();
    });
}


We could also modify a column to be nullable:

public function up()
{
    Schema::table('test', function (Blueprint $table) {
    $table->increments('id');
    $table->string('test_name')->nullable()->change();
    });
}


Renaming Columns To rename a column, you may use the renameColumn method on the schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file:
 

public function up()
{
    Schema::table('test', function (Blueprint $table) {
    $table->renameColumn('test_name', 'Name');
    });
}

 

Related Post