“Specified key was too long error” error in Laravel 5.4

I upgraded my Laravel installation to 5.4 and encountered the error below:

This is actually documented in the official Laravel website here: https://laravel.com/docs/master/migrations#creating-indexes where it fixes the issue. You need to call the Schema::defaultStringLength method within your AppServiceProvider class’ boot method.

However, the documentation does not mention that you need to declare the Schema class at the top, or else you will encounter the error like below:

So be sure to add:  use Schema;

Adding a backslash before Schema also works fine.

I hope this somehow helps :)