VARCHAR max in laravel 5

32,628

Solution 1

There is no "max" constant for VARCHAR in MySQL. Instead you need to define the maximum value of the field yourself.

In order to define max length for a text field just provide max length limit as second parameter:

$table->string('name', 64);

It will result in a VARCHAR column being created in your MySQL database. Keep in mind that if max length is very high, a field of different type might be created instead (mediumtext, longtext).

Solution 2

Use text for varchar(max):

$table->text('name');

Solution 3

So in Laravel you have to use a number of bytes instead of an automatic max. That max length in recent versions of MySQL is 65,535 bytes but the entire row needs to be smaller than that. So if this is the only long text field in the row, you should be fine just subtracting a little from the max and using that. If you're going to have several long text fields in one row you'll want to subtract the length of the other fields and then divide.

Assuming this is the only big text field in your blog field you'd probably be fine just doing:

$table->string('name', 60000);

Solution 4

In app/Auth/Providers set the file AppServiceProvider.php like:

use Illuminate\Support\ServiceProvider;

and in boot function:

Schema::defaultStringLength(191);

Solution 5

because this is a varchar type in mysql or mariadb or ... . VARCHAR supports the maximum size at 65535 characters.

$table->string('name', "65535");
Share:
32,628
JP Foster
Author by

JP Foster

Updated on June 03, 2021

Comments

  • JP Foster
    JP Foster almost 3 years

    I have searched all over the web for a tutorial on varchar max. I have even tried

    $table->string('name', "MAX");
    

    This gives me an error.

    How can I set up varchar max for a blog post for a laravel project. Thanks

  • JP Foster
    JP Foster over 8 years
    I understand this. But online it shows that a blog post should be varchar(max). Since TEXT fields are depreciated. I am looking for a workaround.
  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    max is just a placehoder for the max length value... If you do as I posted in the answer you'll get a column of type varchar(64) - check the database after migration is run
  • Aaron Mason
    Aaron Mason over 2 years
    For MS SQL Server, this is the correct answer - it maps to nvarchar(max).
  • sabvente
    sabvente over 2 years
    This creates 'text' type column in PostgreSQL.