The error you're encountering is related to the maximum key length in MySQL when using the utf8mb4 character set. The utf8mb4 character set uses up to 4 bytes per character, which can exceed the 1000-byte limit for an index in older versions of MySQL or when using certain storage engines.
Here are a few solutions to resolve this issue:
-
Change the Character Set: If you don't need the full range of Unicode characters, you can switch to
utf8, which uses up to 3 bytes per character. However, this is not recommended if you need to support emojis or other 4-byte characters. -
Modify the Key Length: You can specify a shorter length for the indexed column. For example, if you know that the email addresses will not exceed a certain length, you can limit the index to that length.
-
Upgrade MySQL: If possible, upgrade to a newer version of MySQL that supports larger index sizes.
-
Use InnoDB with Barracuda and DYNAMIC/COMPRESSED Row Format: If you're using InnoDB, ensure that the
innodb_file_formatis set toBarracudaand theinnodb_large_prefixis enabled. This allows for larger index key prefixes.
Here's how you can modify your migration to specify a shorter index length:
Schema::create('password_reset_tokens', function (Blueprint $table) {
$table->string('email', 191)->primary(); // Limit the email column to 191 characters
$table->string('token', 255);
$table->timestamp('created_at')->nullable();
});
The 191 character limit is a common workaround because 191 * 4 = 764 bytes, which is under the 1000-byte limit.
Additionally, ensure your database configuration in config/database.php is set to use utf8mb4:
'mysql' => [
'driver' => 'mysql',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
// other configurations...
],
After making these changes, try running your migrations again.