Home ยป Specified Key Was Too Long Error In Laravel

Specified Key Was Too Long Error In Laravel

Last updated on December 24, 2020 by

Generally, the specified key was too long error generated in Laravel after running the command php artisan migrate with a new Laravel setup.

As the error suggested that key was too long then it’s defined somewhere in the Core file of Laravel. So to resolve this error, we need to define more lengths for the keys. Let’s first understand why this error occurs?

Why Specified Key was Too Long Error Occurred?

Our research team has found that this error generated due to the default database character set. We know this “database character set” as a collation in MySQL. Are you getting? Greate! Let’s go ahead.

Laravel officials say: By default, Laravel uses the utf8mb4 character set, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them. You may configure this by calling the Schema::defaultStringLength method within your AppServiceProvider.

Solution 1: For Key was Too Long Error in Laravel?

  1. Go to app/Providers directory and open AppServiceProvider.php file and adjust the boot() method to the following.
  2. Don’t forget to adjust namespace use Illuminate\Support\Facades\Schema; at the top.
use Illuminate\Support\Facades\Schema;

    public function boot()
    {
        Schema::defaultStringLength(191);
    }

Solution 2: For Key was Too Long Error in Laravel?

In the worse case, If the above solution doesn’t work then try the following:

  1. Go to config directory and open database.php file.
  2. In that file, you can see two attributes 'charset' => 'utf8mb4'and 'collation' => 'utf8mb4_unicode_ci'in mysql driver.
  3. Now, change that 2 attributes as following
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
  1. We have changed the config file so we need to flush the old configuration. To do that we need to run two commands php artisan config:clear and php artisan config:cache.
  2. Now, Run php artisan migrate:fresh. Be cautious this command will drop all the tables and re-run all the migrations.

That’s it. You have done very nice job. We hope this post helped you to resolve specified key too long error in Laravel.

Additionally, read our guide:

  1. Best Way to Remove Public from URL in Laravel
  2. Run PHP Artisan Commands On Shared Hosting Servers
  3. How To Calculate Age From Birthdate
  4. Active Directory Using LDAP in PHP or Laravel
  5. How To Use The Laravel Soft Delete
  6. How To Add Laravel Next Prev Pagination
  7. cURL error 60: SSL certificate problem: unable to get local issuer certificate
  8. Difference Between Factory And Seeders In Laravel
  9. Difference Between Events and Observers In Laravel
  10. Session Not Working In Laravel

Please let us know in the comments if everything worked as expected or your issues or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading the post ๐Ÿ™‚

 
 

Leave a Comment