如何使laravel Blueprints morphs方法在指定列之后添加列 [英] How to make laravel Blueprints morphs method to add column after a specified column
问题描述
在创建迁移脚本时,我可以做类似的事情
While creating migration scripts I can do something like this
Schema::table('books', function(Blueprint $table)
{
$table->string('reference')->after('access');
});
这将在访问列之后创建我的参考列.但是,如果我想使用morph,该怎么做.我正在考虑这样做
This will create my reference column after access column. But if I want to use morph how would I do this. I was thinking of doing this
Schema::table('books', function(Blueprint $table)
{
$table->morphs('reference')->after('access');
});
但是,当我尝试运行迁移时,这给了我一个迁移错误.这是因为变形没有after
方法.我正在使用laravel 5.1.我不确定访问后如何获得参考列.任何帮助或建议都会很棒.谢谢
However, this gives me a migration error when I try to run the migration. This is because morphs doesn't have a after
method. I am using laravel 5.1. I am not sure how I could have the reference columns after access. Any help or suggestion would be great. Thank you
推荐答案
$ table-> morphs()只是一个快捷方式.这是它的功能(Laravel 5.5):
$table->morphs() is just a shortcut. this is the function behind it (Laravel 5.5):
/**
* Add the proper columns for a polymorphic table.
*
* @param string $name
* @param string|null $indexName
* @return void
*/
public function morphs($name, $indexName = null)
{
$this->unsignedInteger("{$name}_id");
$this->string("{$name}_type");
$this->index(["{$name}_id", "{$name}_type"], $indexName);
}
因此具有相同的效果:
Schema::table('books', function (Blueprint $table) {
$table->unsignedInteger("reference_id")->after('access');
$table->string("reference_type")->after('reference_id');
$table->index(["reference_id", "reference_type"]);
});
这篇关于如何使laravel Blueprints morphs方法在指定列之后添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!