13

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

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
JP Foster
  • 1,725
  • 4
  • 17
  • 23

5 Answers5

27

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).

dylan-myers
  • 323
  • 3
  • 18
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • 1
    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. – JP Foster Dec 18 '15 at 19:23
  • 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 – jedrzej.kurylo Dec 18 '15 at 20:19
16

Use text for varchar(max):

$table->text('name');
Ryan Shripat
  • 5,574
  • 6
  • 49
  • 77
Augusto Russo
  • 161
  • 1
  • 3
4

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);
Andy Groff
  • 2,660
  • 1
  • 21
  • 25
3

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

use Illuminate\Support\ServiceProvider;

and in boot function:

Schema::defaultStringLength(191);
gjerich
  • 369
  • 3
  • 6
3

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

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