1

I want to convert a field from string to boolean trying this code:

 public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->boolean('email_permission')->change();
        $table->boolean('sms_permission')->change();
    });
}

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

but i got the error below

Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42804]: Datatype mismatch: 7 ERROR:  column 
"email_permission" cannot be cast automatically to type boolean
HINT:  You might need to specify "USING email_permission::boolean".")

this is original migration:

   public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->string('email_permission')->nullable()->default('0');
        $table->string('sms_permission')->nullable()->default('0');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->dropColumn('email_permission');
        $table->dropColumn('sms_permission');
    });
}

how can i change this field type from integer to boolean ?

2
  • please share the original migration Commented Apr 4, 2021 at 8:16
  • 2
    Is the DB type postgresql? Does this help? stackoverflow.com/questions/41149554/… Commented Apr 4, 2021 at 8:21

1 Answer 1

0

i using this way and it work perfectly

 public function up()
{
    Schema::table('users', function (Blueprint $table) {
        DB::statement("ALTER TABLE users 
          ALTER COLUMN email_permission DROP DEFAULT,
          ALTER COLUMN email_permission TYPE BOOLEAN USING email_permission::BOOLEAN,
          ALTER COLUMN email_permission SET DEFAULT FALSE;");
    });
}
Sign up to request clarification or add additional context in comments.

Comments

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.