如何在Laravel 5 Migrate中设置带有时区的TimeStamp [英] How to set TimeStamp with Timezone in Laravel 5 Migrate

查看:322
本文介绍了如何在Laravel 5 Migrate中设置带有时区的TimeStamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在可迁移laravel 5中设置了时间戳,如下所示:

I had set timestamp in migrate laravel 5, like this:

$table->timestamp('verify_account')->nullable();

但是,在postgresql中没有设置时区.我想在Postgresql中设置带有时区的时间戳,怎么做?

But, that is no set with timezone in postgresql. I want to set timestamp with timezone in postgresql, how to do it?

推荐答案

我已经解决了问题..我在laravel供应商目录中添加了一些代码

I had fix my problem.. I add a few code in laravel vendor directory

第一步,打开

vendor/laravel/framework/src/illuminate/Database/Schema/Blueprint.php

vendor/laravel/framework/src/illuminate/Database/Schema/Blueprint.php

然后,添加此内容:

public function timestampz($column)
{
    return $this->addColumn('timestamptz', $column);
}

之后,打开

vendor/laravel/framework/src/illuminate/Database/Schema/Grammars/PostgresGrammar.php

vendor/laravel/framework/src/illuminate/Database/Schema/Grammars/PostgresGrammar.php

然后,添加此内容:

 protected function typeTimestamptz(Fluent $column)
    {
        return 'timestamp(0) with time zone';
    }

使用方法: 在您的迁移文件中,就像这样:

How to use: In your migration file, that is like this:

$table->timestampz('yourcolumntimestampwithtimezone');

更新

  1. 在app/Custom中创建文件,将其命名为CustomBlueprint.php.如果您没有自定义目录,请先创建.

CustomBlueprint.php:

CustomBlueprint.php :

namespace App\Custom;

use Illuminate\Database\Schema\Blueprint;

class CustomBlueprint extends Blueprint {

    public function timestampz($column)
    {
        return $this->addColumn('timestamptz', $column);
    }

}

  1. 创建CustomPgsqlGrammar.php.

CustomPgsqlGrammar.php:

CustomPgsqlGrammar.php:

namespace App\Custom;

use Illuminate\Support\Fluent;
use Illuminate\Database\Schema\Grammars\PostgresGrammar;

class CustomPgsqlGrammar extends PostgresGrammar {

    protected function typeTimestamptz(Fluent $column)
    {
        return 'timestamp(0) with time zone';
    }

}

  1. 在您的迁移文件中:

别忘了

use App\Custom\CustomBlueprint;
use App\Custom\CustomPgsqlGrammar;
use Illuminate\Database\Migrations\Migration;

然后在

Schema :: create

Schema::create

DB::connection()->setSchemaGrammar(new CustomPgsqlGrammar());
$schema = DB::connection()->getSchemaBuilder();

$schema->blueprintResolver(function($table, $callback) {
    return new CustomBlueprint($table, $callback);
});

  1. 使用方法:

  1. How to use:

$ schema-> create('users',function(CustomBlueprint $ table) {

$schema->create('users', function(CustomBlueprint $table) {

$table->timestampz('completing_registration');

}

这篇关于如何在Laravel 5 Migrate中设置带有时区的TimeStamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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