Programming: Laravel Error – Specified Key Was Too Long

A common error I have seen users reporting using Laravel, is the following:

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

The error actually happens because Laravel 5.5 assumes you have MySQL 5.7.7 or above. While on MySQL 5.7.7 the key size was increased than the older versions.

How to fix laravel Specified key was too long

To fix this, find theĀ AppServiceProvider.php file under app/Providers folder, and add the following line before start of the class:

use Illuminate\Support\Facades\Schema;

Then, inside the boot() method under the class, add the following line:

Schema::defaultStringLength(191);

If you restart your laravel migration or the query, it should work fine now.