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

In this tutorial we will see how we can solve laravel migration error like database setting is not correct error 1071 specified key was too long max key length is 767 bytes.

When we are going to migrate then its show key was too long in laravel. So if you want to solve this issue then follow below step. Every laravel beginner developer face a problem like Syntax error or access violation: 1071 Specified key was too long max k when they are going to migrate thier project database.

syntax-error-or-access-vialation-migration-error-laravel

Now We will see hot we can solve database setting is not correct error 1071 specified key was too long max key length is 767 bytes error from our project.

[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

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the bootmethod set a default string length:

app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

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

After that everything should work as normal.