50

It not work, I search so many ways, How can I change length of value in laravel 5

public function up()
{
    Schema::table('articles', function (Blueprint $table) {
        $table->string('name',50)->change();
    });
}

Thank you all for reading!

Sang Trần
  • 2,147
  • 4
  • 13
  • 20

3 Answers3

86

Regarding documentation, your example should work:

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

Just run php artisan migrate command to execute it.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Excuse me! Can you teach me how to change column to foreign key. Thank you for supporting me! – Sang Trần Apr 07 '16 at 08:09
  • 1
    SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto colu mn and it must be defined as a key (SQL: ALTER TABLE articles CHANGE code code VARCHAR(50) NOT NULL, CHANGE name na me VARCHAR(50) NOT NULL, CHANGE sort sort INT AUTO_INCREMENT NOT NULL) – Sang Trần Apr 07 '16 at 08:21
  • @SangTrần, you should start new question. Describe your problem and what did you try there, post your original migration and error text. Me, or other SO guys will help you with it. ;) – Alexey Mezenin Apr 07 '16 at 08:27
  • This doesn't work probably because name is special word in MySQL. I have the same problem for column with name key. – Farid Movsumov Jan 20 '19 at 18:52
  • @SangTrần That doesn't work because laravel does an alter table [table] change. To modify a column with a foreign key constraint, you'll have to use alter table [table] modify (i.e. DB::statement('alter table [table] modify [column] [changes]');). – Verron Knowles Sep 10 '19 at 22:38
14

At least in Laravel 5.6, if it is requiring Doctrine DBAL and you don't want to install it now for any reason, you can try this:

<?php

// ...

Schema::table('users', function ($table) {
    DB::statement('ALTER TABLE users MODIFY COLUMN name VARCHAR(50)');
});
victorf
  • 978
  • 2
  • 17
  • 35
  • 1
    This is the only thing that's worked for me. I tried using doctrine/dbal and it just won't change my columns. But this did! So while not exactly the answer I wanted, it does work and I have spent far too long on this. – dustbuster Dec 11 '19 at 16:29
  • Upon further inspection, what's happening with me is that the doctrine/dbal is changing some of the columns across multiple tables, but not all. This will change every column 100% sure! – dustbuster Dec 11 '19 at 20:04
  • DB::statement('ALTER TABLE users MODIFY COLUMN name VARCHAR(50)'); is enough, no need to wrap it in the Schema::table call – memical Oct 04 '21 at 10:11
3

There is an outstanding (open for multiple years and still being discussed) issue related to Doctrine DBAL (discussion here: https://github.com/laravel/framework/issues/1186).

The deceptive part of this issue is that a ->change() will fail if any column is of type enum (not just the column you are changing!)

If your table has an enum column, you need to revert to using an alter statement (note you don't need to wrap it in a Table::schema:

DB::statement('ALTER TABLE users MODIFY COLUMN name VARCHAR(50)')

Chris
  • 54,599
  • 30
  • 149
  • 186