Home  >  Article  >  PHP Framework  >  How Laravel 7 supports PosgreSQL range types

How Laravel 7 supports PosgreSQL range types

Guanhui
Guanhuiforward
2020-07-20 12:41:282568browse

How Laravel 7 supports PosgreSQL range types

The PosgreSQL range type package written by @belamov provides range type support for the Postgres database:

Schema::create('table', function (Blueprint $table) {
    $table->id();
    // ...
    $table->dateRange('date_range');
    $table->timestampRange('timestamp_range');
    $table->floatRange('float_range');
    // 对于 int4range
    $table->integerRange('integer_range');
    // 对于 int8range
    $table->bigIntegerRange('integer_range');

    // 您可以添加任何修改
    // $table->dateRange('date_range')->nullable();
    // $table->dateRange('date_range')->default('[2010-01-01,2010-01-02)');
});

The main features of this package include:

  • Extends Laravel's PostgresGrammar and PostgresConnection classes to provide a fluent API for range columns.

  • The following Postgres range types are supported: daterange, tsrange, numrange, intrange and timerange.

  • Many convenient query building macros (i.e. whereRangeContains($left,$right)

  • Model attribute conversion

The model attribute conversion provided by this package provides convenience for using 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 about it at belamov/postgres-range More information about this package, complete installation instructions, and view the source code on GitHub. This package has an excellent blog post that will give you a quick introduction to the powerful features available in Postgres range types: Using PostgreSQL Ranges in Laravel 7 .

Recommended tutorial: "Laravel Tutorial"

The above is the detailed content of How Laravel 7 supports PosgreSQL range types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete