簡體   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