Laravel5.7でphp artisan migrateしようとしたら、以下のようなエラー文が吐き出された時。
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 `account` add unique `account_login_id_unique`(`login_id`))
at /home/vagrant/local/zeus/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
/home/vagrant/local/zeus/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
2 PDOStatement::execute()
/home/vagrant/local/zeus/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
Please use the argument -v to see more details.

どうしてーー?

よくみると、SQL: alter table `account` add unique `account_login_id_unique`(`login_id`)とあり、login_idがおかしいと書いています。
$table->string('login_id')->unique();
原因
MySQLのver5.7.7以前の場合、テーブルのindexに指定できるデータ長は最大767byteで超えてしまっているからのようです。
laravelのデフォルトcharsetは”utf8mb4″で、1文字あたり4byteの領域が必要な為、varchar(255)で格納する列を作った場合255*4=1020byteになるからです。
varchar(string)のデータ長を191byteにすれば、191*4=764で767byteに収まるようになります。
app/Providers/AppServiceProvider.phpを開きます。

function boot()の中に以下の一文を追加します。
\Illuminate\Support\Facades\Schema::defaultStringLength(191);
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
\Illuminate\Support\Facades\Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
もう一度php artisan migrateします。
[vagrant@localhost zeus]$ php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2018_11_18_195714_create_account_tables
Migrated: 2018_11_18_195714_create_account_tables
Migrating: 2018_11_18_204842_create_password_history_tables
Migrated: 2018_11_18_204842_create_password_history_tables
できたー
