DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Changing Column Datatype in Laravel - A Step-by-Step Guide

Changing Column Datatype in Laravel: A Step-by-Step Guide

Changing the datatype of a column in a Laravel application's database schema is a common task that developers encounter during the evolution of their projects. Whether you need to switch from one datatype to another or adjust the length of a string column, Laravel provides convenient migration tools to handle these changes seamlessly. In this article, we'll explore how to change the datatype of a column using Laravel migrations.

Step 1: Create a Migration

First, let's create a migration file to define the changes to our database schema. We'll use the make:migration Artisan command to generate a new migration file. Open your terminal or command prompt and run the following command:

php artisan make:migration change_completion_status_datatype_in_donation_product_backups_table
Enter fullscreen mode Exit fullscreen mode

This command will create a new migration file in the database/migrations directory of your Laravel project.

Step 2: Define the Schema Changes

Open the generated migration file in your text editor. Inside the up() method, use the table() method to modify the existing table. Here's an example of how to change the datatype of the completion_status column from boolean to string:

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

class ChangeCompletionStatusDatatypeInDonationProductBackupsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('donation_product_backups', function (Blueprint $table) {
            $table->string('completion_status')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('donation_product_backups', function (Blueprint $table) {
            $table->boolean('completion_status')->nullable()->change();
        });
    }
}
Enter fullscreen mode Exit fullscreen mode

In the up() method, we use the change() method to modify the completion_status column's datatype to string and make it nullable. Similarly, in the down() method, we define how to revert the changes by changing the datatype back to boolean.

Step 3: Run the Migration

Now that we've defined our migration, it's time to run it and apply the changes to the database. Use the migrate Artisan command to execute all pending migrations:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

This command will run all migrations that haven't been run yet, including our new migration for changing the completion_status column's datatype.

Conclusion

By following these simple steps, you can easily change the datatype of a column in your Laravel application's database schema. Laravel's migration system provides a convenient and reliable way to manage database changes, ensuring your application's database structure remains up-to-date with your evolving requirements.

Top comments (0)