How to Change Column Types With Laravel Schema Builder

Sometimes you need to change the column type of an existing column in a your Laravel database. You can do this using migrations. In this example, we are going to change a address column in users table from VARCHAR to TEXT.

Quick Tip : Login Event Handling in Laravel 5

To do this, first we need to generate a migration file,

php artisan make:migration change_address_column_in_users_table --table=users

Now, you can use the following code in your newly created migration file to change column type of address from VARCHAR to TEXT.

public function up()
{
    DB::statement('ALTER TABLE users MODIFY COLUMN address TEXT');
}

public function down()
{
    DB::statement('ALTER TABLE users MODIFY COLUMN address VARCHAR(255)');
}   

 

0 Comments

Leave a Comment.