Laravel 5 中的 VARCHAR 最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34362282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:49:57  来源:igfitidea点击:

VARCHAR max in laravel 5

phpmysqlsqllaravellaravel-5

提问by JP Foster

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

我在网上搜索了有关 varchar max 的教程。我什至试过

$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

如何为 laravel 项目的博客文章设置 varchar max。谢谢

回答by jedrzej.kurylo

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

这将导致在您的 MySQL 数据库中创建一个VARCHAR列。请记住,如果最大长度非常高,则可能会创建不同类型的字段(mediumtext、longtext)。

回答by Augusto Russo

Use text for varchar(max):

对 varchar(max) 使用文本:

$table->text('name');

回答by Andy Groff

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.

因此,在 Laravel 中,您必须使用多个字节而不是自动最大值。最新版本的 MySQL 中的最大长度为 65,535 字节,但整行需要小于该值。因此,如果这是该行中唯一的长文本字段,您只需从最大值中减去一点并使用它就可以了。如果您要在一行中有多个长文本字段,您将需要减去其他字段的长度,然后进行除法。

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

假设这是您博客字段中唯一的大文本字段,您可能只需这样做:

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

回答by gjerich

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

在 app/Auth/Providers 中将文件 AppServiceProvider.php 设置为:

use Illuminate\Support\ServiceProvider;

and in boot function:

并在启动功能中:

Schema::defaultStringLength(191);

回答by mohammadreza gohari

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

因为这是 mysql 或 mariadb 或 ... 中的 varchar 类型。VARCHAR 支持的最大大小为 65535 个字符。

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