I'm working with a Laravel project that has two existing columns that are set to strings and hold a visually pleasing Boolean value of true
or false
.
I'm trying to change these columns from string
to boolean
in a Laravel migration, but need to convert the existing values in the columns, for instance, if the column value for a record has the value of false
, it needs to become 0
.
My current migration to modify these columns doesn't seem to acheive this automatically, how can I achieve this?
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddColumnsToNotificationContactsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('notification_contacts', function (Blueprint $table) {
$table->boolean('canNotify')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('notification_contacts', function (Blueprint $table) {
//
});
}
}
I get:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'false' for column 'canNotify' at row 1 (SQL: ALTER TABLE notification_contacts CHANGE canNotify canNotify TINYINT(1) NOT NULL)