laravel 更改架构构建器中的列长度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13805737/
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
Alter column length in Schema builder?
提问by qwerty
I have two fields i need to increment the character limit on. I're read through the documentation and to my surprise i found no option for it. Is it possible to do? If not, how should i go about solving this?
我有两个字段需要增加字符限制。我通读了文档,令我惊讶的是我没有找到它的选项。有可能吗?如果没有,我应该如何解决这个问题?
I could drop the column and re-create it with the correct properties, but i don't want to loose any data in the database.
我可以删除该列并使用正确的属性重新创建它,但我不想丢失数据库中的任何数据。
回答by ademers
For Laravel 4
对于 Laravel 4
DB::update('ALTER TABLE `my_table` MODIFY `my_column` VARCHAR(<new length>)');
回答by NARKOZ
Use Raw Queries:
使用原始查询:
/**
* Make changes to the database.
*
* @return void
*/
public function up()
{
DB::query('ALTER TABLE mytable MODIFY mycolumn VARCHAR(new-length)');
}
/**
* Revert the changes to the database.
*
* @return void
*/
public function down()
{
DB::query('ALTER TABL mytable MODIFY mycolumn VARCHAR(old-length)');
}
Replace mytable
, mycolumn
, new-length
and old-length
.
更换mytable
,mycolumn
,new-length
和old-length
。
For Laravel 5+ replace DB::query
with DB::statement
.
对于 Laravel 5+ 替换DB::query
为DB::statement
.
回答by Yauheni Prakopchyk
回答by Nate Hat
Use `` for the column name if the name is a keyword for the system. Otherwise you will get an "Syntax error or access violation."
如果名称是系统的关键字,则使用 `` 作为列名称。否则,您将收到“语法错误或访问冲突”。
DB::query("alter table MYTABLE modify `MYCOLUMN` varchar(new-length)");