2

The documentation doesn't say anything about changing data type with migrations.

I have this column in my DB table

$table->smallInteger('driverslicensetype')->nullable();

and I am saving four-digit numbers, but when the number begins with 0 (a zero) for example 0230 it will be saved as 230 in DB. And that is wrong.

Now i want to change this column data type from smallInteger to varchar.

How can i do this through migrations?

1 Answer 1

2

To use the schema changing feature, you need Doctrine DBAL:

composer require doctrine/dbal

Now you can use change():

public function up()
{
    Schema::table('foo', function (Blueprint $table) {
        $table->string('driverlicensetype', 4)->nullable()->change();
    });
}

public function down()
{
    Schema::table('foo', function (Blueprint $table) {
        $table->smallInteger('driverlicensetype')->nullable()->change();
    });
}

If you prefer the raw approach (or if your SQL is more restrictive, like when driverlicensetype is a foreign key), use DB::statement:

public function up()
{
    DB::statement('ALTER TABLE foo ...');
}
// ...
Sign up to request clarification or add additional context in comments.

2 Comments

I got this error: [Doctrine\DBAL\DBALException] Unknown database type enum requested, Doctrine\DBAL\Platforms\MySQL57Platform may not support it.
@lewis4u Please ask this as a new question with the relevant code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.