Postgres Range Type Support for Laravel 7
Published on by Paul Redmond
The PosgreSQL range types package by @belamov provides range type support to Eloquent for the Postgres database:
Schema::create('table', function (Blueprint $table) { $table->id(); // ... $table->dateRange('date_range'); $table->timestampRange('timestamp_range'); $table->floatRange('float_range'); // for int4range $table->integerRange('integer_range'); // for int8range $table->bigIntegerRange('integer_range'); // you can add any modifications // $table->dateRange('date_range')->nullable(); // $table->dateRange('date_range')->default('[2010-01-01,2010-01-02)');});
The main features of this package include:
- Extended Laravel’s
PostgresGrammar
andPostgresConnection
classes to provide a fluent API for range columns - Support for the following Postgres range types:
daterange
,tsrange
,numrange
,intrange
, andtimerange
. - A number of query build macros for convenience (i.e.,
whereRangeContains($left, $right)
- Model property casting
The model property casting provided by this package provides convenience for working with ranges on model instances. For example:
use Belamov\PostgresRange\Ranges\IntegerRange; $range = new IntegerRange(10, 20, '[', ')'); $range->from(); // 10$range->to(); // 20(string) $range; // [10,20)$range->forSql(); // '[10,20)'
You can learn more about this package, get full installation instructions, and view the source code on GitHub at belamov/postgres-range. The package has an excellent blog post to bring you up to speed on the powerful features available in Postgres’ range types: Ranges in Laravel 7 using PostgreSQL.