laravel 5.4 版本改变默认的数据库字符集为utf8mb4。如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情。
否则你在运行 migrations 命令时,可能会碰到下面这个错误:
[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))
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
我们可以在 app\Providers\AppServiceProvider.php 文件里中的 boot 方法里设置一个默认值来解决这个错误:
Schema::defaultStringLength(200);
完整的AppServerProvider.php文件代码如下:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Schema::defaultStringLength(200);
}
public function register()
{
//
}
}