Call to undefined method Illuminate\Database\Schema\MySqlBuilder::defaultStringLength()

后端 未结 5 608
[愿得一人]
[愿得一人] 2021-01-25 09:43

Firstly i was getting an error in

php artisan migrate

as

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key w

5条回答
  •  无人共我
    2021-01-25 09:56

    This is because Laravel 5.4 uses the utf8mb4 character set by default, which includes support for storing “emojis” in the database. You can choose any one from these 2 solutions:

    1) Change

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

    to

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

    2) Edit AppServiceProvider located at App\Providers\AppServiceProvider.php and add the following line to the boot() method and load the Schema facade:

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

    What that will do is actually change the default maximum field length in the database and actually shorten your maximum string length from 255 to maximum of 191 characters (utf8 uses 3 bytes per character while utf8mb4 uses 4 bytes per character your field can now hold 25% less characters 255 * 75% = 191.25). So if you don’t set the string field by hand in the migration the new default will be 191.

提交回复
热议问题