onlinecode

Laravel 6 – 1071 Specified key was too long; max key length is 767 bytes – onlinecode

Laravel 6 – 1071 Specified key was too long; max key length is 767 bytes – onlinecode

In this post we will give you information about Laravel 6 – 1071 Specified key was too long; max key length is 767 bytes – onlinecode. Hear we will give you detail about Laravel 6 – 1071 Specified key was too long; max key length is 767 bytes – onlinecodeAnd how to use it also give you demo for it if it is necessary.

When we migration of the table after the installation of the latest laravel at that time we face this type of error.

Because it’s not 520 bytes, but rather, 2080 bytes, which far exceeds 767 bytes, so we are facing this type of error.

Syntax error or access violation, #1071 – Specified key was too long; max key length is 767 bytes, PHP artisan migrate command error, long max key length is 767 bytes, syntax error or access violation 1071, MySQL error

PHP
IlluminateDatabaseQueryException  : 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'))

  at E:xampphtdocslara_authvendorlaravelframeworksrcIlluminateDatabaseConnection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
      E:xampphtdocslara_authvendorlaravelframeworksrcIlluminateDatabaseConnection.php:459

  2   PDOStatement::execute()
      E:xampphtdocslara_authvendorlaravelframeworksrcIlluminateDatabaseConnection.php:459

  Please use the argument -v to see more details.

Now. let’s go we will solve the above error. first, open the AppServiceProvider.php file from the app/Providers/ directory. after then we will load the schema “use IlluminateSupportFacadesSchema;” on the top header and set the “Schema::defaultStringLength(191);” into boot method.

You can see the below code, therefore, then you can better understand.

PHP
<?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportFacadesSchema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        Schema::defaultStringLength(191);
    }
}
?>

After the above changes, you can again migrate the table and then it will be successfully migrated.Read AlsoLaravel 6 Image Upload Tutorial ExampleLaravel 6 Ajax Image Upload Example TutorialLaravel 6 Multiple Image Upload Example Tutorial

Please follow and like us:

Hope this code and post will helped you for implement Laravel 6 – 1071 Specified key was too long; max key length is 767 bytes – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github