How to Change Column Name and Data Type in Laravel Migration?

How to Change Column Name and Data Type in Laravel Migration?

In this post we will give you information about How to Change Column Name and Data Type in Laravel Migration?. Hear we will give you detail about How to Change Column Name and Data Type in Laravel Migration?And how to use it also give you demo for it if it is necessary.

Hi Artisan,

This post is focused on laravel migration change column name. this example will help you how to change column type in laravel migration. step by step explain how to rename column name in laravel migration. i explained simply about how to change data type in laravel migration. follow bellow step for how to change column name in laravel migration.

I will give you two example of changing data type and rename column name using migration in laravel application.

First of all we need to install “doctrine/dbal” composer package.

Install Composer Package:

composer require doctrine/dbal

After successfully install composer package we can change data type and change column name using migration.

Let’s see bellow example:

Migration for main table:

<?php

use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class CreatePostsTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('posts', function (Blueprint $table) {

$table->bigIncrements('id');

$table->string('title');

$table->text('body');

$table->boolean('is_publish')->default(0);

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('posts');

}

}

Change Data Type using Migration:

body column text data type to long text data type here.

<?php

use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class ChangePostsTableColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('posts', function (Blueprint $table) {

$table->longText('body')->change();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

Rename using Migration:

rename title column to name changed.

Also see:How to Create Table using Migration in Laravel?

<?php

use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class ChangePostsTableColumn extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::table('posts', function (Blueprint $table) {

$table->renameColumn('title', 'name');

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

}

}

I hope it can help you…

Hope this code and post will helped you for implement How to Change Column Name and Data Type in Laravel Migration?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

2  +    =  3

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US