Laravel – Specified key was too long Error

By Dillon Smart · · · 0 Comments

With Laravel 5.4, some users may be hit with the Laravel Specified key was too long Error when migrating the database. This is because in the latest Laravel update, the developers made changes to the default database charset, making it utf8mb4.

If your using MySQL 5.7.7 or higher, you won’t encounter this issue, however older versions of MySQL or MariaDB will have this problem.

You can find the solution to this error in the Laravel Documentation. You will need to make a change to the AppServiceProvider.php file, located in app/Providers/AppServiceProvider.php.

Solving Laravel Specified key was too long

Step One:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; // This line needs to be added

Step Two: 

public function boot()
{
    Schema::defaultStringLength(191); // This line needs to be added
}

Now, you should be able to run your migration as normal. For more development tutorials, check out our Web Development category.

LaravelPHP

0 Comment

Was this helpful? Leave a comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Laravel 9: How to Add a New Column to an Existing Table in a Migration

Updated 1st October 2023

Laravel has a number of features designed to make developing web applications easier and faster. Migrations in Laravel allows us to manage the structure of our databases. In this post, you will learn how to add a new column to an existing table in a migration. Create a new migration file First, we need to

What is PHP?

Updated 1st August 2022

PHP (PHP: Hypertext Preprocessor) is an open-source, general-purpose, and server-side scripting language widely used in web development. PHP is used to manage databases, dynamic content, session tracking, and much more. Let’s answer the question “What is PHP?”. What is PHP used for? Since its first release in 1995, PHP has been seen used in many

Pixel Tracking in PHP

Updated 16th August 2022

When creating email campaigns, it’s important to know how many recipients actually opened your email. This information can help you determine if your efforts are working or if it’s time to change your strategy. Most mail champaign tools such as Mailchimp and SendInBlue use a technique called Pixel Tracking. In this post, you will learn