Search code examples
laraveleloquentvarchar

Laravel's Eloquent: bypass the 191 characters VARCHAR limitation


As indicated by this person, this person and this person, in order to avoid a

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

you have to modify the AppServiceProvider.php of your Laravel project like that:

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;              //add this

class AppServiceProvider extends ServiceProvider {
    public function boot() {
        Schema::defaultStringLength(191);           //and this
    }

    public function register() { }
}

But the drawback of this method is that the VARCHARs I use are limited to 191 characters, and I want to have a field longer than that. How should I do?

Thank you for your help.


Solution

  • Actually on your migration file you can define the size. Example

    $table->string('field_name', 200);