SQLSTATE 42000: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes with PHP

2

After I created the project with the command composer create-project --prefer-dist laravel/laravel laravel-scout I made the following changes in the .env file

DB_DATABASE=course
DB_USERNAME=root
DB_PASSWORD=1234

Then I created the database with the same name as course

Then I ran the php artisan migrate command within the project, and generated this error;

PS C:\Users\wladimir\Desktop\php\laravel-scout> php artisan migrate
Migration table created successfully.


  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table 'users' add unique 'users_email_unique'('email')
  )



  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes


PS C:\Users\wladimir\Desktop\php\laravel-scout>
  • Can anyone explain to me what the Error means?
  • Could someone explain me how to fix the error?
  • Note: I'm running Xampp , it's an application that installs PHP and Mysql.

        
    asked by anonymous 11.10.2017 / 19:53

    1 answer

    10

    I've also gone through this, the best solution I found was to change the AppServiceProvider , so I do not have to adjust the fields every time I create a new migration . According to official documentation :

      

    Laravel uses the utf8mb4 character set by default, which includes   support for storing "emojis" in the database. If you are   running a version of MySQL older than version 5.7.7 or   MariaDB prior to version 10.2.2, you may need to configure   manually set the length of the default string generated by migrations to   MySQL creates indexes for them. You can set this by calling   the Schema :: defaultStringLength method in the AppServiceProvider :

    To resolve this, follow the steps below:

  • Edit the app \ Providers \ AppServiceProvider.php
  • Add namespace use Illuminate\Support\Facades\Schema;
  • Within the boot method, add Schema::defaultStringLength(191);
  • Final Result of the file:

    use Illuminate\Support\Facades\Schema;
    
    public function boot()
    {
        Schema::defaultStringLength(191);
    }
    

    For more information consult documentation: link

        
    18.10.2017 / 16:17