{"id":20388547,"url":"https://github.com/efureev/laravel-support-db","last_synced_at":"2025-04-12T10:41:58.692Z","repository":{"id":56975573,"uuid":"333310574","full_name":"efureev/laravel-support-db","owner":"efureev","description":"Support for ext PostgreSQL specific features","archived":false,"fork":false,"pushed_at":"2025-02-24T19:41:11.000Z","size":145,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T02:09:52.695Z","etag":null,"topics":["laravel","pg","php","postgres"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/efureev.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-27T05:10:20.000Z","updated_at":"2025-02-24T19:40:19.000Z","dependencies_parsed_at":"2022-08-21T12:20:13.049Z","dependency_job_id":null,"html_url":"https://github.com/efureev/laravel-support-db","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Flaravel-support-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Flaravel-support-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Flaravel-support-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/efureev%2Flaravel-support-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/efureev","download_url":"https://codeload.github.com/efureev/laravel-support-db/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248557006,"owners_count":21124156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","pg","php","postgres"],"created_at":"2024-11-15T03:11:23.059Z","updated_at":"2025-04-12T10:41:58.670Z","avatar_url":"https://github.com/efureev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Laravel Database Support\n\n![](https://img.shields.io/badge/php-\u003e=8.2|8.3-blue.svg)\n![](https://img.shields.io/badge/Laravel-\u003e=11.0-red.svg)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/5c8b9e85897f4c65b5a017d16f6af6cb)](https://app.codacy.com/manual/efureev/laravel-support-db)\n![PHP Database Laravel Package](https://github.com/efureev/laravel-support-db/workflows/PHP%20Database%20Laravel%20Package/badge.svg)\n[![Latest Stable Version](https://poser.pugx.org/efureev/laravel-support-db/v/stable?format=flat)](https://packagist.org/packages/efureev/laravel-support-db)\n[![Total Downloads](https://poser.pugx.org/efureev/laravel-support-db/downloads)](https://packagist.org/packages/efureev/laravel-support-db)\n[![Maintainability](https://api.codeclimate.com/v1/badges/97e244f2aa0ad5b425c5/maintainability)](https://codeclimate.com/github/efureev/laravel-support-db/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/97e244f2aa0ad5b425c5/test_coverage)](https://codeclimate.com/github/efureev/laravel-support-db/test_coverage)\n\n## Description\n\n## Install\n\n```bash\ncomposer require efureev/laravel-support-db \"^2.0\"\n```\n\n## Contents\n\n- [Ext Column Types](#ext-column-types)\n    - [Bit](#bit)\n    - [GeoPoint](#geo-point)\n    - [GeoPath](#geo-path)\n    - [IP Network](#ip-network)\n    - [Ranges](#ranges)\n    - [UUID](#uuid)\n    - [XML](#xml)\n    - [Array of UUID](#array-of-uuid)\n    - [Array of Integer](#array-of-integer)\n    - [Array of Text](#array-of-text)\n- [Column Options](#column-options)\n    - [Compression](#compression)\n- [Views](#views)\n- [Indexes](#indexes)\n    - [Partial indexes](#partial-indexes)\n    - [Unique Partial indexes](#unique-partial-indexes)\n- [Extended Schema](#extended-schema)\n    - [Create like another table](#create-like-another-table)\n    - [Create as another table with full data](#create-as-another-table-with-full-data)\n    - [Create as another table with data from select query](#create-as-another-table-with-data-from-select-query)\n    - [Drop Cascade If Exists](#drop-cascade-if-exists)\n- [Extended Query Builder](#extended-query-builder)\n    - [Update records and return deleted records` columns](#update-records-and-return-updated-records-columns)\n    - [Delete records and return deleted records` columns](#delete-records-and-return-deleted-records-columns)\n- [Extensions](#extensions)\n\n### Ext Column Types\n\n#### Bit\n\nBit String.\n[Doc](https://www.postgresql.org/docs/current/datatype-bit.html).\n\n```php\n$table-\u003ebit(string $column, int $length = 1);\n```\n\n#### Geo Point\n\nPoints are the fundamental two-dimensional building block for geometric types.\n[Doc](https://www.postgresql.org/docs/current/datatype-geometric.html#id-1.5.7.16.5).\n\n```php\n$table-\u003egeoPoint(string $column);\n```\n\n#### Geo Path\n\nPaths are represented by lists of connected points.\n[Doc](https://www.postgresql.org/docs/current/datatype-geometric.html#id-1.5.7.16.9).\n\n```php\n$table-\u003egeoPoint(string $column);\n```\n\n#### IP Network\n\nThe IP network datatype stores an IP network in CIDR notation.\n[Doc](https://www.postgresql.org/docs/current/datatype-net-types.html).\n\nIPv4 = 7 bytes  \nIPv6 = 19 bytes\n\n```php\n$table-\u003eipNetwork(string $column);\n```\n\n#### Ranges\n\nThe range data types store a range of values with optional start and end values. They can be used e.g. to describe the\nduration a meeting room is booked.\n[Doc](https://www.postgresql.org/docs/current/rangetypes.html).\n\n```php\n$table-\u003edateRange(string $column);\n$table-\u003etsRange(string $column);\n$table-\u003etimestampRange(string $column);\n```\n\n#### UUID\n\nThe `primaryUUID` can be used to store UUID-type as primary key.\n\n```php\n$table-\u003eprimaryUUID(); // create PK UUID-column with name `id`\n$table-\u003eprimaryUUID('custom_name'); // create PK UUID-column with name `custom_name`\n```\n\nThe `generateUUID` can be used to store UUID-type with/without index (or FK).\n\nOn a row creating generates a value with `uuid_generate_v4()` by extension `uuid-ossp`.\n\n```php\n// create UUID-column with name `id`. Generate UUID-value by DB.\n$table-\u003egenerateUUID();\n\n// create UUID-column with name `cid`. Generate UUID-value by DB.\n$table-\u003egenerateUUID('cid');\n\n// create UUID-column with name `cid`. NOT generate UUID-value by DB. Set `nullable`. Default value: `NULL`. \n$table-\u003egenerateUUID('id', null);\n\n// create UUID-column with name `cid`. NOT generate UUID-value by DB. Set `nullable`. Default value: `NULL`. Create Index by this column.\n$table-\u003egenerateUUID('fk_id', null)-\u003eindex();\n\n // create UUID-column with name `fk_id`. NOT generate UUID-value by DB.\n$table-\u003egenerateUUID('fk_id', false);\n\n// create UUID-column with name `fk_id`. Generate UUID-value by DB with custom value.\n$table-\u003egenerateUUID('fk_id', fn($column)=\u003e'uuid_generate_v5()');\n\n// create UUID-column with name `fk_id`. Generate UUID-value by DB with custom value.\n$table-\u003egenerateUUID('fk_id', new Expression('uuid_generate_v2()'));\n```\n\n#### XML\n\nThe xml data type can be used to store an XML document.\n[Doc](https://www.postgresql.org/docs/current/datatype-xml.html).\n\n```php\n$table-\u003exml(string $column);\n```\n\n#### Array of UUID\n\nThe array of UUID data type can be used to store an array of IDs (uuid type).\n\n```php\n$table-\u003euuidArray(string $column);\n```\n\n#### Array of Integer\n\nThe array of integer data type can be used to store a list of integers.\n\n```php\n$table-\u003eintArray(string $column);\n```\n\n#### Array of Text\n\nThe array of text data type can be used to store a list of string.\n\n```php\n$table-\u003etextArray(string $column);\n```\n\n### Column Options\n\n#### Compression\n\nPostgreSQL 14 introduced the possibility to specify the compression method for toast-able data types. You can choose\nbetween the default method `pglz`, the recently added `lz4` algorithm and the value `default` to use the server default\nsetting.\n[Doc](https://www.postgresql.org/docs/current/storage-toast.html).\n\n```php\n$table-\u003estring('col')-\u003ecompression('lz4');\n```\n\n### Views\n\n#### Create views\n\n```php\n// Facade methods:\nSchema::createView('active_users', \"SELECT * FROM users WHERE active = 1\");\nSchema::createView('active_users', \"SELECT * FROM users WHERE active = 1\", true) ;\nSchema::createViewOrReplace('active_users', \"SELECT * FROM users WHERE active = 1\");\n\n// Schema methods:\nuse \\Php\\Support\\Laravel\\Database\\Schema\\Postgres\\Blueprint;\n\nSchema::create('users', function (Blueprint $table) {\n    $table\n        -\u003ecreateView('active_users', \"SELECT * FROM users WHERE active = 1\")\n        -\u003ematerialize();\n});\n```\n\n#### Dropping views\n\n```php\n// Facade methods:\nSchema::dropView('active_users');\nSchema::dropViewIfExists('active_users');\n```\n\n### Indexes\n\n#### Partial indexes\n\nSee: https://www.postgresql.org/docs/current/indexes-partial.html\n\nExample:\n\n```php\nuse \\Php\\Support\\Laravel\\Database\\Schema\\Postgres\\Blueprint;\nSchema::create('table', static function (Blueprint $table) {\n    $table-\u003estring('code'); \n    $table-\u003esoftDeletes();\n    $table\n        -\u003epartial('code')\n        -\u003ewhereNull('deleted_at');\n});\n```\n\nIf you want to delete partial index, use this method:\n\n```php\nuse \\Php\\Support\\Laravel\\Database\\Schema\\Postgres\\Blueprint;\n\nSchema::create('table', static function (Blueprint $table) {\n    $table-\u003edropPartial(['code']);\n});\n```\n\n#### Unique Partial indexes\n\nExample:\n\n```php\nuse \\Php\\Support\\Laravel\\Database\\Schema\\Postgres\\Blueprint;\nSchema::create('table', static function (Blueprint $table) {\n    $table-\u003estring('code'); \n    $table-\u003esoftDeletes();\n    $table\n        -\u003euniquePartial('code')\n        -\u003ewhereNull('deleted_at');\n});\n```\n\nIf you want to delete partial unique index, use this method:\n\n```php\nuse \\Php\\Support\\Laravel\\Database\\Schema\\Postgres\\Blueprint;\n\nSchema::create('table', static function (Blueprint $table) {\n    $table-\u003edropUniquePartial(['code']);\n});\n```\n\n`$table-\u003edropUnique()` doesn't work for Partial Unique Indexes, because PostgreSQL doesn't define a partial (ie\nconditional) UNIQUE constraint. If you try to delete such a Partial Unique Index you will get an error.\n\n```SQL\nCREATE UNIQUE INDEX CONCURRENTLY examples_new_col_idx ON examples (new_col);\nALTER TABLE examples\n    ADD CONSTRAINT examples_unique_constraint USING INDEX examples_new_col_idx;\n```\n\nWhen you create a unique index without conditions, PostgresSQL will create Unique Constraint automatically for you, and\nwhen you try to delete such an index, Constraint will be deleted first, then Unique Index.\n\n### Extended Schema\n\n#### Create like another table\n\nCreate a table from a source-table. Creates a structure only.  \n`includingAll` copies all dependencies from source-table.\n\nCreating will be without a data.\n\n```php\nSchema::create('target_table', function (Blueprint $table) {\n    $table-\u003elike('source_table')-\u003eincludingAll(); \n    $table-\u003eifNotExists();\n});\n```\n\n#### Create as another table with full data\n\nCopy a table from a source-table. Copy only columns and a data. Without indexes and so on...\n\n```php\nSchema::create('target_table', function (Blueprint $table) {\n    $table-\u003efromTable('source_table'); \n});\n```\n\n#### Create as another table with data from select query\n\nCreate a table from a select query. Copy only columns and a data. Without indexes and so on...\n\n```php\nSchema::create('target_table', function (Blueprint $table) {\n    $table-\u003efromSelect('select id, name from source_table');\n});\n\n// or\n\nSchema::create('target_table', function (Blueprint $table) {\n    $table-\u003efromSelect(\n        'select t1.id, t2.enabled, t2.extra from source_table t1 ' .\n        'join source_table_2 t2 on t1.id = t2.src_id ' .\n        'where t2.enabled = true'\n    );\n});\n\n// or\n\n$tbl = 'source_table';\nSchema::create(\n    $tbl,\n    static function (Blueprint $table) {\n        $table-\u003estring('key', 16)-\u003eprimary();\n        $table-\u003estring('title');\n        $table-\u003einteger('sort')-\u003eindex();\n    }\n);\n\n// or\n\nSchema::create(self::TGT_TABLE, function (Blueprint $table) use ($tbl) {\n    Schema::createExtensionIfNotExists('uuid-ossp');\n\n    $table-\u003efromSelect(\n        'select uuid_generate_v4() as id, key, title, sort from ' . $tbl\n    );\n});\n\n// or\n\nSchema::create(self::TGT_TABLE, function (Blueprint $table) use ($tbl) {\n    Schema::createExtensionIfNotExists('uuid-ossp');\n\n    $table-\u003efromSelect(\n        'select uuid_generate_v4() as id, * ' . $tbl\n    );\n});\n```\n\n#### Drop Cascade If Exists\n\nAutomatically drop objects that depend on the table (such as views, indexes, seqs), and in turn all objects that depend\non those objects.\n\n```php\nSchema::dropIfExistsCascade('table');\n```\n\n### Extended Query Builder\n\n#### Update records and return updated records` columns\n\n```php\n$list = Model::toBase()-\u003eupdateAndReturn(['deleted_at' =\u003e now()], 'id', 'name');\n```\n\n```php\n$list = Model::where(['enabled' =\u003e true])-\u003eupdateAndReturn(['enabled' =\u003e false], 'id');\n```\n\n#### Delete records and return deleted records` columns\n\n```php\n$list = Model::toBase()-\u003edeleteAndReturn('id', 'name');\n```\n\n```php\n$list = Model::where(['enabled' =\u003e true])-\u003edeleteAndReturn('id');\n```\n\n### Extensions\n\n#### Create Extensions\n\nThe Schema facade supports the creation of extensions with the `createExtension` and `createExtensionIfNotExists`\nmethods:\n\n```php\nSchema::createExtension('uuid-ossp');\nSchema::createExtensionIfNotExists('uuid-ossp');\n```\n\n#### Dropping Extensions\n\nTo remove extensions, you may use the `dropExtensionIfExists` methods provided by the Schema facade:\n\n```php \nSchema::dropExtensionIfExists('tablefunc');\n```\n\nYou may drop many extensions at once by passing multiple extension names:\n\n```php\nSchema::dropExtensionIfExists('tablefunc', 'fuzzystrmatch');\n```\n\n-----\n\n## Usage\n\n### Simple example\n\n```php\n\u003c?php\n\nuse Illuminate\\Support\\Facades\\Schema;\nuse Php\\Support\\Laravel\\Database\\Schema\\Postgres\\Blueprint;\n\nSchema::create(\n    'test_table',\n    static function (Blueprint $table) {\n        $table-\u003eprimaryUUID();\n        $table-\u003egenerateUUID('id', null);\n        $table-\u003etsRange('range');\n        $table-\u003enumeric('num');\n        \n    }\n);\n```\n\n## Test\n\n```bash\ncomposer test\ncomposer test-cover # with coverage\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fefureev%2Flaravel-support-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fefureev%2Flaravel-support-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fefureev%2Flaravel-support-db/lists"}