DEV Community


Specifying MySQL Index Size in Laravel

Originally published at on ・1 min read

Specifying MySQL Index Size in Laravel

When writing migrations, if you've come across an error specified key was too long; max key length is 3072 bytes, this might help you as it did when I faced this issue on my job board.

Laravel's Blueprint $table accepts a raw statement, so you can define it this way

Schema::table('table_name', function (Blueprint $table) {
    $table->index([DB::raw('field_name(50)')], 'index_name');
Enter fullscreen mode Exit fullscreen mode

index_name is optional as Laravel neatly prefixes table name to field name, so it becomes table_name_field_name

As to why this error occurs and finding the ideal index size, this is a great answer on Stackoverflow

Discussion (0)