sunaoka / laravel-postgres-extension
Extended PostgreSQL driver for Laravel.
v1.4.0
2023-06-22 07:03 UTC
Requires
- php: ^7.3 || ^8.0
- ext-json: *
- illuminate/database: ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- nunomaduro/larastan: ^1.0 || ^2.0
- orchestra/testbench: ^6.19 || ^7.0 || ^8.0
- phpstan/phpstan-mockery: ^1.1
README
Installation
composer require sunaoka/laravel-postgres-extension
Configurations
php artisan vendor:publish --tag=postgres-extension
Features
-
Range Types
- int4range — Range of integer
- int8range — Range of bigint
- numrange — Range of numeric
- tsrange — Range of timestamp without time zone
- tstzrange — Range of timestamp with time zone
- daterange — Range of date
-
RETURNING
- UPDATE
- DELETE
-
Caching "information_schema" table.
Usage
Table
CREATE TABLE some_models ( id bigserial PRIMARY KEY NOT NULL, code text NOT NULL, term tsrange NOT NULL, CONSTRAINT code_uq UNIQUE (code) );
Model
<?php namespace App\Models; class SomeModel extends \Sunaoka\LaravelPostgres\Eloquent\Model { protected $casts = [ 'term' => \Sunaoka\LaravelPostgres\Eloquent\Casts\TsRangeCast::class, // tsrange ]; }
Range Types
tsrange — Range of timestamp without time zone
$some = new SomeModel(); $some->code = 'some code'; $some->term = new TsRange('2020-10-01 00:00:00', '2020-10-01 23:59:59'); $some->save();
insert into "some_models" ("code", "term") values ('some code', '[2020-10-01 00:00:00,2020-10-01 23:59:59)') returning "id";
$some = SomeModel::find(1); echo $some->term->lower()->format('Y-m-d H:i:s'); // lower() or from() // => 2020-10-01 00:00:00 echo $some->term->upper()->format('Y-m-d H:i:s'); // upper() or to() // => 2020-10-01 23:59:59
RETURNING
$some = SomeModel::whereId(1) ->returning(['*']) ->update([ 'term' => new TsRange('2020-09-01 00:00:00', '2020-09-01 23:59:59'), ]); echo get_class($some); // => Illuminate\Database\Eloquent\Collection echo get_class($some->first()); // => App\Models\SomeModel
update "some_models" set "term" = '[2020-09-01 00:00:00,2020-09-01 23:59:59)' where "id" = '1' returning *
Caching "information_schema" table.
Permanently cache the results for a table like the one below.
select * from information_schema.tables where table_schema = 'public' and table_name = 'some_models'