How do I solve a "Specified key was too long error" in Laravel?
Laravel 5.4 made a change to the default database character set. For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
In this article we'll explain how to solve this issue.
Steps
Step 1. Log into the cPanel of your Laravel site.
Step 2. Under category Files, click Filemanager.
Step 3. Navigate to the app/Providers folder.
Step 4. Select the AppServiceProvider.php file and click Edit.
Step 5. Inside the boot method set a default string length:
public function boot() {
Schema::defaultStringLength(191);
}
Step 6. Click Save Changes and the problem should be fixed!