sunaoka / laravel-postgres-range
PostgreSQL Range Types for Laravel.
v1.2.0
2024-04-24 06:18 UTC
Requires
- php: ^8.1
- ext-json: *
- illuminate/database: ^8.67 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.67 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- larastan/larastan: ^1.0 || ^2.0
- orchestra/testbench: ^6.19 || ^7.0 || ^8.0 || ^9.0
- phpstan/phpstan-mockery: ^1.1
This package is auto-updated.
Last update: 2024-04-24 06:18:39 UTC
README
Installation
composer require sunaoka/laravel-postgres-range
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
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; use Illuminate\Database\Eloquent\Model; use Sunaoka\LaravelPostgres\Eloquent\Casts\Int4RangeCast; use Sunaoka\LaravelPostgres\Eloquent\Casts\TsRangeCast; class SomeModel extends Model { protected $casts = [ 'int4' => Int4RangeCast::class, // int4range 'ts' => TsRangeCast::class, // tsrange ]; }
Range Types
use Sunaoka\LaravelPostgres\Types\Bounds\Lower; use Sunaoka\LaravelPostgres\Types\Bounds\Upper; use Sunaoka\LaravelPostgres\Types\Int4Range; use Sunaoka\LaravelPostgres\Types\TsRange; $some = new SomeModel(); $some->int4 = new Int4Range(1, 10, Lower::Inclusive, Upper::Exclusive); $some->ts = new TsRange('2020-10-01 00:00:00', '2020-10-01 23:59:59', Lower::Inclusive, Upper::Exclusive); $some->save();
insert into "some_models" ( "int4", "ts") values ('[1,10)', '[2020-10-01 00:00:00,2020-10-01 23:59:59)') returning "id";
$some = SomeModel::find(1); echo $some->int4->lower(); // lower() or from() // => 1 echo $some->int4->upper(); // upper() or to() // => 10 echo $some->ts->lower()->format('Y-m-d H:i:s'); // lower() or from() // => 2020-10-01 00:00:00 echo $some->ts->upper()->format('Y-m-d H:i:s'); // upper() or to() // => 2020-10-01 23:59:59