How to make laravel Blueprints morphs method to add column after a specified column

北战南征 提交于 2020-01-02 05:37:22

问题


While creating migration scripts I can do something like this

Schema::table('books', function(Blueprint $table)
        {
            $table->string('reference')->after('access');
        });

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

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


回答1:


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

So this has the same effect:

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"]);
});


来源:https://stackoverflow.com/questions/46493616/how-to-make-laravel-blueprints-morphs-method-to-add-column-after-a-specified-col

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!