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

后端 未结 5 600
[愿得一人]
[愿得一人] 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:49

    If Laravel < 5.4 1. Find AppServiceProvider in Laravel Project 2. Comment line or remove Schema::defaultStringLength(191) in boot method

    0 讨论(0)
  • 2021-01-25 09:51

    Find AppServiceProvider in Laravel Project

    1.First Add Facade

    use Illuminate\Support\Facades\Schema;
    

    2.Add Below lines in boot function

    function boot() {
      Schema::defaultStringLength(191);
    }
    
    0 讨论(0)
  • 2021-01-25 09:55

    defaultStringLength is introduced in Laravel v5.4. Reference.

    You can update the Laravel version

    OR

    You specify the length as $table->string('coumname', 255); You cannot specify more than 255. If you leave the length parameter it will assign 255 by default

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-25 10:02
    1. Add use Illuminate\Support\Facades\Schema; to your AppServiceProvider.php file in App/Providers folder.

    2. Add Schema::defaultStringLength(191); to the boot function.

    0 讨论(0)
提交回复
热议问题