Home » Laravel: Change Column Type In Migration

Laravel: Change Column Type In Migration

Last updated on June 1, 2021 by

At some level of project creation, you might want to change the column type in Laravel migration. In this, article we will see the steps to change the column type in Laravel migration. Let’s just dive into it.

Change Column Type In Laravel Migration

The Laravel provides the change() method to change the column type and attributes. Let’s take a simple example.

Let’s create a new migration for users table and then we will change any column type and attributes into it for better understanding.

php artisan make:migration create_users_table

The above command will create a new migration in database/migrations directory. We have added the following code into the newly created migration.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->text('title')->nullable();
            $table->text('first_name', 10);
            $table->text('last_name');
            $table->string('email', 20)->unique();
            $table->string('password')->nullable();
            $table->text('gender')->nullable();        
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Now, let’s migrate the database using php artisan migrate command and the users table will be created into our database.

Let’s suppose, now we want to change the first_name column type from text to string and increase the size from 10 to 50. To do that we need to follow the following steps:

  1. First and most important thing is to check whether or not your project has doctrine/dbal library, if not then please install it first by running the following command:
composer require doctrine/dbal
  1. Create new migration file to update the column
php artisan make:migration update_users_table --table="users"

A new migration file will be created into the database/migrations directory.

  1. Let’s now change the type of first_name column up() method and don’t forget to add the reverse effect in the down() method.

    The up() method will be executed when we run the php artisan migrate command and the down() method will be executed when we run the php artisan migrate:rollback command. Thus, you can balance between the migrate and rollback command.
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('first_name', 50)->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->text('first_name', 10)->change();
        });
    }
}

  1. Finally, run the migrate command to update the table.
php artisan migrate

Additionally, read our guide:

  1. Base Table Or View Already Exists In Laravel Migration
  2. Add Column After A Column In Laravel Migration
  3. Specified Key Was Too Long Error In Laravel
  4. AJAX PHP Post Request With Example
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Difference Between Factory And Seeders In Laravel
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Calculate Age From Birthdate
  11. How To Check Laravel PHP Version
  12. How To Handle Failed Jobs In Laravel
  13. How To Remove WooCommerce Data After Uninstall
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel
  16. How To Pass Laravel URL Parameter
  17. Laravel Run Specific Migration

That’s it from our end. We hope this article helped you to learn how to change column names and rename foreign key columns in Laravel migration.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment