繁体   English   中英

Laravel在php artisan make:auth之后无法迁移php artisan

[英]Laravel Failing on php artisan migrate after php artisan make:auth

我决定测试新的Laravel。 因此,我从基本命令开始:

$ laravel new blog

$ php artisan make:auth

$ php artisan migrate

然后我编辑了AppServiceProvider.php文件,并将默认字符串长度添加到启动方法中

use Illuminate\Support\Facades\Schema;

function boot()
{
    Schema::defaultStringLength(191);
}

但是我仍然有一个错误:

  [Symfony\Component\Debug\Exception\FatalErrorException]
  Call to undefined method Illuminate\Database\Schema\MySqlBuilder::defaultStringLength()

您打错了课。 类Illuminate \\ Support \\ Facades \\ Schema没有方法defaultStringLength,但是可以通过类Illuminate \\ Database \\ Schema \\ MySqlBuilder来实现。 检查文档: MySqlBuilder文档 架构文档

Laravel 5.4中引入了defaultStringLength

参考

这是因为Laravel 5.4默认使用utf8mb4字符集,该字符集支持在数据库中存储“表情符号”。 您可以从以下两种解决方案中选择一种:

1)变更

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

2)编辑位于App \\ Providers \\ AppServiceProvider.php的AppServiceProvider,并将以下行添加到boot()方法中,并加载Schema外观:

use Illuminate\Support\Facades\Schema;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
    Schema::defaultStringLength(191);
}

实际执行的操作是更改数据库中默认的最大字段长度,并将最大字符串长度从255缩短为最多191个字符(utf8每个字符使用3个字节,而utf8mb4每个字符使用4个字节,您的字段现在可以容纳25%少字符255 * 75%= 191.25)。 因此,如果您在迁移过程中未手动设置字符串字段,则新的默认值为191。

在应用程序/提供者中打开AppServiceProviders

开机功能

  Schema::defaultStringLength(191);

并在顶部添加以下类

use Illuminate\Support\Facades\Schema;

虽然没有必要在laravel 5.6中使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM