Imran Abbas
Imran Abbas

Reputation: 805

How to change column data type in laravel 5.6?

I am trying to change column data type using laravel 5.6.

I have a table in which two columns have a data type of text but I would like to change it to longtext. I have tried following:

...and then created the migration 2019_12_23_065820_change_response_column_data_type_in_log_requests_table.php for log_requests table.

...and then the following script

public function up()
{
    Schema::table('log_requests', function (Blueprint $table) {
        $table->longText('request')->nullable()->change();
        $table->longText('response')->nullable()->change();
    });
}

But it is not changing the column's data type. Can someone guide me? Where am I wrong so that I can fix it? Thank you.

EDITED

After requesting for migration in comment, I added migration script:

public function up()
{
    Schema::create('log_requests', function (Blueprint $table) {
        $table->increments('id');
        $table->bigInteger('user_id')->nullable()->unsigned();
        $table->string('api_name')->nullable();
        $table->string('url')->nullable();
        $table->string('method')->nullable();
        $table->string('ip_address')->nullable();
        $table->string('status_code')->nullable();
        $table->string('duration')->nullable();
        $table->text('request')->nullable();
        $table->text('response')->nullable();
        $table->timestamps();
    });
}

Upvotes: 8

Views: 2402

Answers (4)

Amit Sharma
Amit Sharma

Reputation: 1795

you can do this with the help of this function. You can create a new migration and change column type

public function up()
{
Schema::table('log_requests', function (Blueprint $table) {
    $table->longText('request')->change();
    $table->longText('response')->change();
});

You need to install doctrine/dbal to make this work

composer require doctrine/dbal

Upvotes: 0

Fahd Yousri
Fahd Yousri

Reputation: 411

This is an issue with how doctorine treats texts, it doesn't differentiate long medium or normal text however it's doable by changing the size attribute.

$table->string('request', 4294967295)->change();

as per @andfelzapata in the issue https://github.com/laravel/framework/issues/9636

Upvotes: 2

Immeyti
Immeyti

Reputation: 545

Just change the column comment, for example:

$table->mediumText('myColumn')->comment(' ')->change(); // up
$table->text('myColumn')->comment('')->change(); // down

Upvotes: 4

N69S
N69S

Reputation: 17205

You can avoid using Dbal if you go for the direct query

public function up()
{
    \DB::statement('alter table log_requests modify request longtext null;');
    \DB::statement('alter table log_requests modify response longtext null;');
}

public function down()
{
    \DB::statement('alter table log_requests modify request text null;');
    \DB::statement('alter table log_requests modify response text null;');
}

Upvotes: 1

Related Questions