The PosgreSQL range types package by @belamov provides range type support to Eloquent for the Postgres database:
1Schema::create('table', function (Blueprint $table) { 2 $table->id(); 3 // ... 4 $table->dateRange('date_range'); 5 $table->timestampRange('timestamp_range'); 6 $table->floatRange('float_range'); 7 // for int4range 8 $table->integerRange('integer_range'); 9 // for int8range10 $table->bigIntegerRange('integer_range');11 12 // you can add any modifications13 // $table->dateRange('date_range')->nullable();14 // $table->dateRange('date_range')->default('[2010-01-01,2010-01-02)');15});
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:
1use Belamov\PostgresRange\Ranges\IntegerRange;2 3$range = new IntegerRange(10, 20, '[', ')');4 5$range->from(); // 106$range->to(); // 207(string) $range; // [10,20)8$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.