问题
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