Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davy_yg's avatar
Level 27

migration

When trying to migrate table I get this:

C:\xampp\htdocs\shopee_lvl\framework>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 users2 add unique users2_email_unique(email))

at C:\xampp\htdocs\shopee_lvl\framework\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) {

664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668|

Exception trace:

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes") C:\xampp\htdocs\shopee_lvl\framework\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

2 PDOStatement::execute() C:\xampp\htdocs\shopee_lvl\framework\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

Please use the argument -v to see more details.

C:\xampp\htdocs\shopee_lvl\framework>

The table does exist yet I cannot view it from the table tree structure.

0 likes
4 replies
djay's avatar

Hi @davy_yg

Inside the App/Providers folder go and

edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

Don't forget to put use Illuminate\Support\Facades\Schema;

After that everything should work as normal. thanks

davy_yg's avatar
Level 27

Thanks it works now. Should I do that every time I start using laravel to make the migration works?

  1. Also, what is the point of remembering token?

    $table->rememberToken();

  2. I also get this error message when trying to use varchar in my migration:

C:\xampp\htdocs\shopee_lvl\framework>php artisan migrate Migration table created successfully.

BadMethodCallException : Method Illuminate\Database\Schema\Blueprint::varchar does not exist.

at C:\xampp\htdocs\shopee_lvl\framework\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php:99 95| */ 96| public function __call($method, $parameters) 97| { 98| if (! static::hasMacro($method)) {

99| throw new BadMethodCallException(sprintf( 100| 'Method %s::%s does not exist.', static::class, $method 101| )); 102| } 103|

Exception trace:

1 Illuminate\Database\Schema\Blueprint::__call("varchar") C:\xampp\htdocs\shopee_lvl\framework\database\migrations\2014_1012000000_create_users_table.php:19

2 CreateUsersTable::{closure}(Object(Illuminate\Database\Schema\Blueprint)) C:\xampp\htdocs\shopee_lvl\framework\vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php:164

Please use the argument -v to see more details.

C:\xampp\htdocs\shopee_lvl\framework>

CreateUsersTable.php

public function up()
    {
    Schema::create('users2', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email')->unique();
        $table->varchar('username', 20);
        $table->varchar('password', 50);
        $table->biginteger('no_ktp', 30);
        $table->varchar('foto_ktp', 30);
        $table->varchar('foto_profile', 30);
        $table->rememberToken();
        $table->timestamps();
      });
     }
djay's avatar
djay
Best Answer
Level 2

Hi @davy_yg

why you are creating users2 table

It is better to follow convention already set by laravel framework while learning as well as development because going out of convention will definetly through errors and will be wastage of your time

which version of Laravel you are using?

go through the proper documentation here:-

https://laravel.com/docs/5.6

You can change the version to match yours

Use string instead of varchar like this:

$table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

Thanks I hope this will answer

Please or to participate in this conversation.