Call to undefined method Illuminate\Database\Schema\MySqlBuilder::defaultStringLength()

Gags picture Gags · Feb 8, 2017 · Viewed 15.9k times · Source

Firstly i was getting an error in

php artisan migrate

as

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

and Then i found a post on laracasts to fix this as

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

After adding this, when i run

php artisan optimize

then i am presented with new error as below

Call to undefined method Illuminate\Database\Schema\MySqlBuilder::defaultStringLength()

Please assist in getting rid of all these errors.

Answer

Saumini Navaratnam picture Saumini Navaratnam · Feb 8, 2017

defaultStringLength is introduced in Laravel v5.4. Reference.

You can update the Laravel version

OR

You specify the length as $table->string('coumname', 255); You cannot specify more than 255. If you leave the length parameter it will assign 255 by default