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

November 20, 2017

When you run the migration command in Laravel you will get the following error

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was t
oo long; max key length is 767 bytes (SQL: alter table `users` add unique `
users_email_unique`(`email`))

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

Reason for the error
Laravel 5.4 has done changes for the default charset of the database. It is now utf8mb4


How to fix this error

1 Upgrate your MySql Database to v5.7.7 and higher

2 You can edit the AppServiceProvider.php file which is located app>Providers location

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
         Schema::defaultStringLength(191);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}