0

I'm using laravel 5.6.17 and php 7.2; When I run php artisan migrate command, I receive the following error.

For more information, The default tables "users" and "migrations" have been created in the declared database.

Doesn't laravel support php 7.2??

Image: laravel 5.6.17 php artisan migrate error

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`))
at D:\xampp\htdocs\laravel\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")
D:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2   PDOStatement::execute()
D:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
Please use the argument -v to see more details.
Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33
  • 1
    Your problem is in your SQL, not in artisan, not in PHP, not in Laravel. You tried to index something and it won't fit the index. Post your migration, don't take random code from internet as solutions because what you got is not the solution, it's just going to hide the problem. – N.B. Apr 23 '18 at 06:27
  • 1
    This is not a duplicate because the answer provided is crap and incorrect. – N.B. Apr 23 '18 at 07:31

2 Answers2

5

In the AppServiceProvider.php,you include this code top of the file.

use Illuminate\Support\Facades\Schema;

And you add this code in boot method

Schema::defaultStringLength(191);
Kuldeep Mishra
  • 3,846
  • 1
  • 21
  • 26
4

Yes, I got the answer on the following link...

Just go the following file from your root directory <ROOT>/app/Providers/AppServiceProvider.php

After that:

1) import schema at the top section of the file: use Illuminate\Support\Facades\Schema;

2) and define the length of string in the boot method: Schema::defaultStringLength(191);

// Import Schema
use Illuminate\Support\Facades\Schema;
// ...

class AppServiceProvider extends ServiceProvider
{

public function boot()
{
    // Add the following line
    Schema::defaultStringLength(191);
}

// ...

}
Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33
  • 1
    What does `Schema::defaultStringLength(191)` do exactly? Why does it fix the problem? Do you understand what the problem is? – N.B. Apr 23 '18 at 06:27
  • It fixes the following error `PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")` – Fatema Tuz Zuhora Apr 23 '18 at 06:34
  • Do you **understand** what `Schema::defaultStringLength(191)` DOES at all? Do you **understand** why there's a problem? – N.B. Apr 23 '18 at 06:52
  • as far we know, Database Schema is a set of formulas which described how the database is constructed; here `Schema::defaultStringLength(191)` define the Length of String which didn't cross the limit 767 bytes and as well as the following error has been solved in this way. – Fatema Tuz Zuhora Apr 23 '18 at 07:03
  • 1
    The real solution to your problem is to add another column, `email_hash` fore example, and then **hash** the value of email, store it to that column and make that column unique. Your index will be of a fixed length. You won't have to resort to hacky solutions that you don't understand and that can ruin your database. That's why you need to post *relevant code* which is the migration. And don't accept answers that you don't understand and that aren't explained. That guy just copied some random crap code and gave it to you, and you trusted him. Zero explanation behind it. – N.B. Apr 23 '18 at 07:28
  • the column for email is already unique; would you please write the Schema for the up() function in "users" table? – Fatema Tuz Zuhora Apr 23 '18 at 07:43
  • Sorry, but no. You won't read with comprehension. Your email is of variable length, I suggested what to do so you can use *fixed* length. Language barrier seems to prevent you from understanding what's going on. Good luck and all the best. – N.B. Apr 23 '18 at 07:58
  • 1
    I define the length in the `CreateUsersTable` class under the up() function instead of defining it to the `AppServiceProvider` class. Here it is: `$table->string('email', 191)->unique();` – Fatema Tuz Zuhora Apr 23 '18 at 08:55