Basic Laravel 9 Course is available now! ๐ŸŽ‰ Get your seat now

Laravel 8 Specified key was too long error

#laravel #tutorials

Laravel 5.4 made a change to the default database character set, and itโ€™s now utf8mb4 which includes support for storing emojis

Banner Long Key Error

Since Laravel 5.4, It made a change to the default database character set to uses utf8mb4 which includes support for storing emojis.

  • If you are using MariaDB or older versions of MySQL you may face this error when trying to run migrations:
[Illuminate\Database\QueryException]
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))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
  • In order to fix this problem, you have to make a change in your AppServiceProvider.php file and inside the boot method set a default string length:


 use Illuminate\Support\Facades\Schema; # add on the top or after

  public function boot()
  {
	  Schema::defaultStringLength(191); 
  } 
  • Everything should work as normal, please share this posts If it's helpful to you.