How to create an Hstore column type in Laravel 4?

谁说我不能喝 提交于 2020-02-22 08:21:38

问题


I'm using the Schema Builder inside of Migrations in Laravel 4 to manage my Postgres database during development. I've run into a problem. I want to utilize the Postgres Hstore column type in my table but I can't figure it out.

There doesn't seem to be a "custom" column type in the Schema Builder (that I can find), so this was my attempt inside of a migration. This didn't work for me.

Schema::create('entities', function(Blueprint $table)
{
    $table->bigIncrements('id');
    $table->string('type');
    $table->string('name');
    $table->text('data')->nullable();
    $table->timestamps();
    $table->softDeletes();
});
DB::raw("ALTER TABLE `entities` CHANGE COLUMN `data` `data` hstore NULL;");

I also tried this instead of the last command. No luck.

DB::raw("ALTER TABLE `entities` ALTER `data` TYPE hstore;");

Note: I did already run the command CREATE EXTENSION hstore in my database.


回答1:


This can be done using the Builder::blueprintResolver function

$builder = DB::connection()->getSchemaBuilder();

    $builder->blueprintResolver(function($table, $callback) {
        return new CustomPostgresBlueprint($table, $callback);
    });

    $builder->create('record_sets', function(CustomPostgresBlueprint $table) {
        $table->increments('id');
        $table->hstore('schema');
        $table->timestamps();
    });

You'll also need to implement a typeHstore function within a custom PostgresGrammer subclass. FOr full implementation details, look here



来源:https://stackoverflow.com/questions/19079840/how-to-create-an-hstore-column-type-in-laravel-4

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