qwerty
qwerty

Reputation: 5246

Alter column length in Schema builder?

I have two fields i need to increment the character limit on. I're read through the documentation and to my surprise i found no option for it. Is it possible to do? If not, how should i go about solving this?

I could drop the column and re-create it with the correct properties, but i don't want to loose any data in the database.

Upvotes: 19

Views: 11852

Answers (4)

NARKOZ
NARKOZ

Reputation: 27911

Use Raw Queries:

/**
 * Make changes to the database.
 *
 * @return void
 */
public function up()
{
  DB::query('ALTER TABLE mytable MODIFY mycolumn VARCHAR(new-length)');
}

/**
 * Revert the changes to the database.
 *
 * @return void
 */
public function down()
{
  DB::query('ALTER TABL mytable MODIFY mycolumn VARCHAR(old-length)');
}

Replace mytable, mycolumn, new-length and old-length.

For Laravel 5+ replace DB::query with DB::statement.

Upvotes: 24

Yauheni Prakopchyk
Yauheni Prakopchyk

Reputation: 10912

For Laravel 5:

Schema::table('users', function($table)
{
    $table->string('name', 50)->change();
});

Check docs for further info.

Upvotes: 17

ademers
ademers

Reputation: 967

For Laravel 4

DB::update('ALTER TABLE `my_table` MODIFY `my_column` VARCHAR(<new length>)');

Upvotes: 26

Nate Hat
Nate Hat

Reputation: 418

Use `` for the column name if the name is a keyword for the system. Otherwise you will get an "Syntax error or access violation."

DB::query("alter table MYTABLE modify `MYCOLUMN` varchar(new-length)");

Upvotes: 1

Related Questions