【问题标题】:Laravel Failing on php artisan migrate after php artisan make:authLaravel 在 php artisan make:auth 之后 php artisan 迁移失败
【发布时间】:2017-10-12 20:44:39
【问题描述】:

我决定测试新的 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()

【问题讨论】:

标签: php mysql database laravel authentication


【解决方案1】:

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

在开机功能中

  Schema::defaultStringLength(191);

并在顶部添加以下类

use Illuminate\Support\Facades\Schema;

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

【讨论】:

    【解决方案2】:

    defaultStringLength 是在 Laravel 5.4 中引入的。

    Reference

    【讨论】:

    • 好的,但是如何在没有defaultStringLength的情况下进行注册,它不起作用,也不会在users表中插入数据。 @AguDondo
    • 我将 laravel 5.2 升级到 5.4 ,但我仍然有同样的错误@AguDondo
    【解决方案3】:

    你调用了错误的类。类 Illuminate\Support\Facades\Schema 没有方法 defaultStringLength,但安装类 Illuminate\Database\Schema\MySqlBuilder 可以做到。 检查文档: MySqlBuilder Docs Schema Docs

    【讨论】:

    • 当我添加类 Illuminate\Database\Schema\MySqlBuilder 而不是类 Illuminate\Support\Facades\Schema 时,我有一个错误,找不到类 'App\Providers\Schema'
    【解决方案4】:

    这是因为 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-26
      • 2019-11-06
      • 2019-05-24
      • 1970-01-01
      • 2016-08-09
      • 2017-01-25
      相关资源
      最近更新 更多