Search code examples
databaseschemabuilderlaravelalter

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.


Solution

  • 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.