Addressing the "Laravel Migration: Unique Key Is Too Long" Issue
When migrating a Laravel users table, developers may encounter an error indicating that the specified unique key is too long. Despite explicitly specifying a second parameter in the unique() method, as suggested by a Laravel issue thread, the error persists.
The underlying issue revolves around the length of the email column. Laravel's default length for string columns is 255 characters, which can be insufficient for email addresses.
Solution
To resolve this issue, specify a smaller length for the email column. The recommended default length is 250 characters:
$table->string('email', 250);
However, it's worth noting that this issue has been addressed in Laravel 5.4. To apply a solution for this version:
For Laravel 5.4 and Later
use Illuminate\Database\Schema\Builder; public function boot() { Builder::defaultStringLength(191); }
This sets a default string length of 191 characters for string columns, which eliminates the need to manually specify column lengths.
The above is the detailed content of How to Fix the 'Laravel Migration: Unique Key Is Too Long' Error?. For more information, please follow other related articles on the PHP Chinese website!