Laravel supports many different databases and therefore has to limit itself to the lowest common denominator of all databases. PostgreSQL, however, offers a ton more functionality which is being added to Laravel by this extension.
Installation
You can install the package via composer:
composer require tpetry/laravel-postgresql-enhanced
Features
Migration
Extensions
Create Extensions
The Schema
facade supports the creation of extensions with the createExtension
and createExtensionIfNotExists
methods:
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::createExtension('tablefunc');
Schema::createExtensionIfNotExists('tablefunc');
Dropping Extensions
To remove extensions, you may use the dropExtension
and dropExtensionIfExists
methods provided by the Schema
facade:
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropExtension('tablefunc');
Schema::dropExtensionIfExists('tablefunc');
You may drop many extensions at once by passing multiple extension names:
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropExtension('tablefunc', 'fuzzystrmatch');
Schema::dropExtensionIfExists('tablefunc', 'fuzzystrmatch');
Views
Create Views
The Schema
facade supports the creation of views with the createView
and createViewOrReplace
methods. The definition of your view can be a sql query string or a query builder instance:
use Illuminate\Support\Facades\DB;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::createView('users_with_2fa', 'SELECT * FROM users WHERE two_factor_secret IS NOT NULL');
Schema::createViewOrReplace('users_without_2fa', DB::table('users')->whereNull('two_factor_secret'));
If you need to create recursive views the createRecursiveView
and createRecursiveViewOrReplace
methods can be used like in the former examples but you need to provide the available columns as last parameter:
use Illuminate\Support\Facades\DB;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
// TODO simple example explaining the concept
Schema::createView('viewname', 'SELECT id, col1, col2 FROM ....', ['id', 'col1', 'col2']);
Schema::createViewOrReplace('viewname', 'SELECT id, col1, col2 FROM ....', ['id', 'col1', 'col2']);
Dropping Views
To remove views, you may use the dropView
and dropViewIfExists
methods provided by the Schema
facade. You don't have to distinguish normala and recursive views:
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropView('myview');
Schema::dropViewIfExists('myview');
You may drop many views at once by passing multiple view names:
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropExtension('myview1', 'myview2');
Schema::dropExtensionIfExists('myview1', 'myview2');
Indexes
Drop If Exists
In addition to the Laravel methods to drop indexes, methods to drop indexes if they exist have been added. The methods dropIndexIfExists
, dropPrimaryIfExists
, dropSpatialIndexIfExists
and dropSpatialIndexIfExists
match the semantics of their laravel originals.
Create Partial Indexes
A partial index is an index built over a subset of a table; the subset is defined by a condition. The index contains entries only for those table rows that satisfy the condition. Partial indexes are a specialized feature, but there are several situations in which they are useful. Take for example you want to make the email address column of your users table unique and you are using soft-deletes. This is not possible because by deleting a user and creating it again the email address is used twice. With partial indexes this can be done by limiting the index to only untrashed rows:
use Illuminate\Database\Query\Builder;
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('users', function(Blueprint $table) {
$table->partialUnique(['email'], fn (Builder $condition) => $condition->whereNull('deleted_at'));
});
Partial Indexes are created with the partialIndex
, partialSpatialIndex
, partialUnique
methods. The name of the index is created automatically like for normal laravel indexes, if you want to change the name pass it's value as third parameter.
Special attention is needed for dropping partial unique indexes, you need to use the special partial drop methods: dropPartialUnique
and dropPartialUniqueIfExists
.
Column Types
Bit Strings
The bit string data types store strings of 0s and 1s. They can be used to e.g. store bitmaps.
// @see https://www.postgresql.org/docs/current/datatype-bit.html
$table->bit(string $column, int $length = 1);
$table->varbit(string $column, ?int $length = null);
Case Insensitive Text
The case insensitive text type is used to store a text that will be compared case insensitive. It can be used to e.g. store and compare e-mail addresses.
// @see https://www.postgresql.org/docs/current/citext.html
$table->caseInsensitiveText(string $column);
IP Networks
The ip network datatype stores an ip network in cidr notation.
// @see https://www.postgresql.org/docs/current/datatype-net-types.html
$table->ipNetwork(string $column);
Hstore
The hstore data type is used store key/value pairs within a single PostgreSQL value. The new json data type is better in all aspects, so hstore should only be used for compatibility with old applications.
// @see https://www.postgresql.org/docs/current/hstore.html
$table->hstore(string $column);
International Product Numbers
The international product number data types are used to store common product numbers types and validate them before saving.
// @see https://www.postgresql.org/docs/current/isn.html
$table->europeanArticleNumber13(string $column);
$table->internationalStandardBookNumber(string $column);
$table->internationalStandardBookNumber13(string $column);
$table->internationalStandardMusicNumber(string $column);
$table->internationalStandardMusicNumber13(string $column);
$table->internationalStandardSerialNumber(string $column);
$table->internationalStandardSerialNumber13(string $column);
$table->universalProductNumber(string $column);
Label Tree
The ltree data type stores a label as its position in a tree. This provides an easy way to manage a tree without performance and complexity disadvantages compared to alternative solutions.
// @see https://www.postgresql.org/docs/current/ltree.html
$table->labelTree(string $column);
Ranges
The range data types store a range of values with optional start and end values. They can be used e.g. to describe the duration a meeting room is booked.
// @see https://www.postgresql.org/docs/current/rangetypes.html
$table->bigIntegerRange(string $column);
$table->dateRange(string $column);
$table->decimalRange(string $column);
$table->integerRange(string $column);
$table->timestampRange(string $column);
$table->timestampTzRange(string $column);
XML
The xml data type can be used to store an xml document.
// @see https://www.postgresql.org/docs/current/datatype-xml.html
$table->xml(string $column);
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.