JP Foster
JP Foster

Reputation: 1845

VARCHAR max in laravel 5

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

Upvotes: 14

Views: 56970

Answers (5)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40899

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

Upvotes: 27

mohammadreza gohari
mohammadreza gohari

Reputation: 160

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

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

Upvotes: 3

gjerich
gjerich

Reputation: 409

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

use Illuminate\Support\ServiceProvider;

and in boot function:

Schema::defaultStringLength(191);

Upvotes: 3

Augusto Russo
Augusto Russo

Reputation: 161

Use text for varchar(max):

$table->text('name');

Upvotes: 16

Andy Groff
Andy Groff

Reputation: 2670

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

Upvotes: 4

Related Questions