A new update is available soon. We're working on new UI

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:
<?php
 
namespace App\Providers;
 
+ use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
 
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
 
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
+ Schema::defaultStringLength(191);
}
}
  • Everything should work as normal, please share this posts If it's helpful to you.