在创建迁移脚本时,我可以做这样的事情
Schema::table('books', function(Blueprint $table)
{
$table->string('reference')->after('access');
});
Run Code Online (Sandbox Code Playgroud)
这将在访问列之后创建我的引用列.但是,如果我想使用变形,我将如何做到这一点.我在想这样做
Schema::table('books', function(Blueprint $table)
{
$table->morphs('reference')->after('access');
});
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行迁移时,这会给我一个迁移错误.这是因为变形没有after方法.我正在使用laravel 5.1.我不确定如何在访问后获得引用列.任何帮助或建议都会很棒.谢谢
sti*_*ihl 13
$ table-> morphs()只是一个捷径.这是它背后的功能(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);
}
Run Code Online (Sandbox Code Playgroud)
所以这有同样的效果:
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"]);
});
Run Code Online (Sandbox Code Playgroud)