PHP code example of tpetry / laravel-postgresql-enhanced
1. Go to this page and download the library: Download tpetry/laravel-postgresql-enhanced library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tpetry / laravel-postgresql-enhanced example snippets
use Illuminate\Database\Migrations\Migration;
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Schema\Concerns\ZeroDowntimeMigration;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
class Test123 extends Migration
{
use ZeroDowntimeMigration;
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('user', function (Blueprint $table) {
$table->string('name', 128)->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user', function (Blueprint $table) {
$table->string('name', 32)->change();
});
}
}
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::createExtension('tablefunc');
Schema::createExtensionIfNotExists('tablefunc');
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropExtension('tablefunc');
Schema::dropExtensionIfExists('tablefunc');
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropExtension('tablefunc', 'fuzzystrmatch');
Schema::dropExtensionIfExists('tablefunc', 'fuzzystrmatch');
Schema::createFunction('search_user', ['pattern' => 'text'], ['id' => 'int', 'email' => 'text'], 'plpgsql', "
BEGIN
RETURN QUERY select user_id, contactemail from users where name ilike '%' || pattern || '%';
END;
");
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropFunction('sales_tax');
Schema::dropFunctionIfExists('sales_tax');
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('projects', function (Blueprint $table): void {
$table->trigger('rollup_quota', 'update_quota_by_projects()', 'AFTER INSERT OR DELETE');
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('projects', function (Blueprint $table): void {
$table->dropTrigger('update_quota');
$table->dropTriggerIfExists('update_quota');
});
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'));
use Illuminate\Support\Facades\DB;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::createView('users_with_2fa', DB::table('users')->select('id')->whereNull('two_factor_secret'), ['user_id']);
use Illuminate\Support\Facades\DB;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
// TODO simple example explaining the concept
Schema::createRecursiveView('viewname', 'SELECT id, col1, col2 FROM ....', ['id', 'col1', 'col2']);
Schema::createRecursiveViewOrReplace('viewname', 'SELECT id, col1, col2 FROM ....', ['id', 'col1', 'col2']);
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropView('myview');
Schema::dropViewIfExists('myview');
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropView('myview1', 'myview2');
Schema::dropViewIfExists('myview1', 'myview2');
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::createMaterializedView('users_with_2fa', 'SELECT * FROM users WHERE two_factor_secret IS NOT NULL');
Schema::createMaterializedView('users_with_2fa', DB::table('users')->whereNull('two_factor_secret'));
Schema::createMaterializedView('users_with_2fa', DB::table('users')->select('id')->whereNull('two_factor_secret'), columns: ['user_id']);
Schema::createMaterializedView('very_slow_query_materialized', 'SELECT ...', withData: false);
Schema::dropMaterializedView('users_with_2fa');
Schema::dropMaterializedViewIfExists('users_with_2fa');
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('users', function(Blueprint $table) {
$table->uniqueIndex('email');
});
use Illuminate\Database\Migrations\Migration;
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
return new class extends Migration
{
public $withinTransaction = false;
public function up(): void
{
Schema::table('blog_visits', function (Blueprint $table) {
$table->index(['url', 'ip_address'])->concurrently();
});
}
};
use Illuminate\Database\Query\Builder;
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::create('subscriptions', function(Blueprint $table) {
$table->id('user_id');
$table->timestampTz('cancelled_at');
$table->uniqueIndex(['user_id', 'cancelled_at'])->nullsNotDistinct();
});
use Illuminate\Database\Query\Builder;
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('users', function(Blueprint $table) {
$table->uniqueIndex('email')->where("deleted_at IS NULL");
// or:
$table->uniqueIndex('email')->where(fn (Builder $condition) => $condition->whereNull('deleted_at'));
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('users', function(Blueprint $table) {
// The query "SELECT firstname, lastname FROM users WHERE email = '[email protected]'" can be executed as an index-only scan without loading the table data
$table->index('email')->
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('invoices', function(Blueprint $table) {
$table->index(['target', 'division', 'date'])->ifNotExists();
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('bookmarks', function(Blueprint $table) {
$table->index('data')->algorithm('gin')->with(['fastupdate' => false]);
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('book', function (Blueprint $table) {
$table->fullText(['title', 'description'])
->language('spanish')
->weight(['A', 'B']);
});
use Tpetry\PostgresqlEnhanced\Query\Builder;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::createDomain('price', 'numeric(9,2)');
Schema::createDomain('price', 'numeric(9,2)', 'VALUE >= 0');
Schema::createDomain('price', 'numeric(9,2)', fn (Builder $query) => $query->where('VALUE', '>=', 0));
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
// To drop the validation condition:
Schema::changeDomainConstraint('price', null);
// To change validation condition:
Schema::changeDomainConstraint('price', 'VALUE > 0');
Schema::changeDomainConstraint('price', fn (Builder $query) => $query->where('VALUE', '>', 0));
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropDomain('price');
Schema::dropDomainIfExists('price');
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::dropDomain('price', 'license_plate');
Schema::dropDomainIfExists('price', 'license_plate');
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('sessions', function (Blueprint $table): void {
// make the table unlogged
$table->unlogged();
// make the table crash-safe again
$table->unlogged(false);
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('sessions', function (Blueprint $table): void {
$table->with([
// Tune statistics generation for tables with millions of records
'autovacuum_analyze_scale_factor' => 0.02,
// Tune table for frequent UPDATE statements
'fillfactor' => 90,
]);
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('books', function (Blueprint $table): void {
// @see https://www.postgresql.org/docs/current/storage-toast.html
$table->string('summary')->compression('lz4');
});
use Tpetry\PostgresqlEnhanced\Schema\Blueprint;
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
Schema::table('users', function (Blueprint $table): void {
$table->boolean('acl_admin')->initial(false);
$table->boolean('acl_read')->initial(false)->default(true);
});
Schema::table('users', function (Blueprint $table): void {
$table->jsonb('email')->using('jsonb_build_array(email)')->change();
});
use Illuminate\Support\Facades\DB;
DB::transaction(function() {
User::lazyByCursor()->each(function (User $user) {
dump($user);
});
// Maximum 500 rows should be loaded into memory for every chunk.
User::lazyByCursor(500)->each(function (User $user) {
dump($user);
});
// Lazy loading rows also works for the query builder.
DB::table('users')->where('active', true)->lazyByCursor()->each(function (object $user) {
dump($user);
});
});
$query->whereIntegerArrayMatches($column, string $query);
$query->orWhereIntegerArrayMatches($column, string $query);
// The tags column should have values 3, 4, 5 or 6 and not 7.
$query->whereIntegerArrayMatches('tags', '3&4&(5|6)&!7');
$query->orderBy($column, string $direction = 'asc'|'desc', string $nulls = 'default'|'first'|'last');
$query->orderByNullsFirst($column, string $direction = 'asc'|'desc', string $nulls = 'default'|'first'|'last');
$query->orderByNullsLast($column, string $direction = 'asc'|'desc', string $nulls = 'default'|'first'|'last');
// Sort the table by the age descending with all NULL values presented last.
$query->orderBy('age', 'desc', nulls: 'last');
$query->orderByNullsLast('age', 'desc');
$query->orderByVectorSimilarity($column, $vector, string $distance = 'cosine'|'l2');
// The five rows with the highest similarity to the provided embeddings.
$query->orderByVectorSimilarity('embeddings', [0.9569, 0.1113, 0.0107])->limit(5);
use Illuminate\Database\Eloquent\Model;
use Tpetry\PostgresqlEnhanced\Eloquent\Concerns\RefreshDataOnSave;
class Example extends Model
{
use RefreshDataOnSave;
// ...
}
$example = Example::create(['text' => 'test']);
dump($example); // ['id' => 1, 'text' => 'test', 'text_uppercase' => 'TEST']
$example->fill(['text' => 'test2'])->save();
dump($example); // ['id' => 1, 'text' => 'test2', 'text_uppercase' => 'TES2T']
use Illuminate\Database\Eloquent\Model;
use Tpetry\PostgresqlEnhanced\Eloquent\Concerns\AutomaticDateFormat;
class Example extends Model
{
use AutomaticDateFormat;
// ...
}
use Illuminate\Database\Eloquent\Model;
use Tpetry\PostgresqlEnhanced\Eloquent\Concerns\AutomaticDateFormatWithMilliseconds;
class Example extends Model
{
use AutomaticDateFormatWithMilliseconds;
// ...
}
BlogVisit::select([
'url',
new TimestampBin('created_at', DateInterval::createFromDateString('5 minutes')),
new Count('*'),
])->groupBy(
'url',
new TimestampBin('created_at', DateInterval::createFromDateString('5 minutes'))
);
use Tpetry\PostgresqlEnhanced\Expressions\Uuid7;
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->uuid()->default(new Uuid7())->unique();
$table->text('text');
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.