如何在Laravel 4中创建Hstore列类型? [英] How to create an Hstore column type in Laravel 4?

查看:98
本文介绍了如何在Laravel 4中创建Hstore列类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel 4的Migrations中使用Schema Builder在开发过程中管理我的Postgres数据库.我遇到了一个问题.我想在表中使用Postgres Hstore列类型,但无法弄清楚.

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.

在Schema Builder中似乎没有自定义"列类型(我可以找到),因此这是我在迁移过程中的尝试.这对我不起作用.

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

注意:我已经在数据库中运行了命令CREATE EXTENSION hstore.

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

推荐答案

这可以使用Builder::blueprintResolver函数

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

您还需要在自定义PostgresGrammer子类中实现typeHstore函数.有关完整的实施详细信息,请此处

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

这篇关于如何在Laravel 4中创建Hstore列类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆