In this tutorial you will learn about the How to Fix Laravel Specified key was too long error and its application with practical example.
How to Fix Laravel Specified key was too long error
In Laravel, sometimes when we run the following command we get “Specified key was too long error” as shown in the below screenshot, in this tutorial I will show you how to fix the “Specified key was too long error” error.
Easiest way to fix error is to locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file
1 |
use Illuminate\Support\Facades\Schema; |
and inside the boot method set a default string length as given below –
1 |
Schema::defaultStringLength(191); |
So this is how my AppServiceProvider file looks like now –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // Schema::defaultStringLength(191); } /** * Register any application services. * * @return void */ public function register() { // } } |
Now delete tables created and run the following command again –
1 |
php artisan migrate |
I hope this will fix “Specified key was too long error” error.