Laravel Syntax error or access violation: 1071 Specified key was too long error fix
Hi friends, In this article I am going to show you to fix a common error in Laravel. When developing a Laravel or Lumen project most developers will have this specified key was too long error. This issue will occur when you try to migrate your tables to database because older versions of your MariaDB or MySQL. The error will be like as below. Laravel Specified Key Was Too Long Fix
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes.....(and your SQL table details)
So I will show you a quick fix for both Laravel and Lumen.
You have to open a file called AppServiceProvider.php located under app/Providers. Now you have to add these following codes to fix it.
If you haven’t imported schema to the file, First import it after the namespace
use Illuminate\Support\Facades\Schema;
Now you will find a method called boot. Add following line inside the method.
Schema::defaultStringLength(191);
I will provide an example AppServiceProvider.php will whole code below.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
}
public function boot()
{
Schema::defaultStringLength(191);
}
}
Thats it! if you have any questions please comment below. Or if you want more articles, go to below link.