Skip to content

Laravel DB PortableWrite Once. Run on Any Database.

Cross-database query builder macros, portable schema tools, migration commands, and parallel database mirrors for Laravel 12 & 13.

Overview ​

When developing Laravel applications across different database engines—such as moving from CockroachDB to MatrixOne, or supporting MySQL, PostgreSQL, and SQLite simultaneously—developers encounter subtle incompatibilities:

  • JSON values are extracted as text: Numeric comparisons like flags->ratio > 0.8 or ordering by numbers fail or sort alphabetically ("10" before "2").
  • Sorting nulls: MySQL has no NULLS LAST syntax; PostgreSQL sorts nulls first by default in descending order.
  • Aggregates and Rollups: group by ... with rollup vs group by rollup (...) vs union all.
  • Search and Indexes: GIN indexes, trigram similarity, unaccent matching, and expression indexes differ dramatically.

Laravel DB Portable bridges these differences. It compiles query macros and schema blueprints to the exact syntax required by the active database connection.


Installation ​

Install the package via Composer:

bash
composer require vuthaihoc/laravel-db-portable

The package requires PHP 8.2+ and Laravel 12 or 13. The service provider is auto-discovered.

Supported Databases & Drivers ​

DatabaseFamilyDriver PackageDriver Key
PostgreSQL (14 - 17+)pgsqlBuilt-in Laravel driverpgsql
CockroachDB (v25 - v26+)pgsqlvuthaihoc/cockroachdb-laravel (^2.5)crdb
MySQL (8.0 - 8.4+)mysqlBuilt-in Laravel drivermysql
MariaDB (10.6 - 11+)mysqlBuilt-in Laravel drivermariadb
MatrixOne (4.2+)mysqlvuthaihoc/laravel-matrixone (^1.2)matrixone
SQLite (3.35+)sqliteBuilt-in Laravel driversqlite
XTDB (Experimental)xtdbvuthaihoc/laravel-xtdb2xtdb

Strict Mode ​

By default, when a database engine does not support a specific feature (for example, expression indexes on MariaDB/MatrixOne, or time-travel reads on SQLite), the package executes a safe fallback and logs a warning once per message:

text
[db-portable] asOfTime() is not supported on SQLite. Skipped.

To enforce strict compatibility during automated tests or CI, enable strict mode in config/db-portable.php or dynamically:

php
config(['db-portable.strict' => true]);

In strict mode, unsupported operations immediately throw a \RuntimeException instead of logging and falling back.


Publishing Configuration ​

Publish the configuration file using Artisan:

bash
php artisan vendor:publish --tag=db-portable-config

The file config/db-portable.php contains settings for strict mode, mirrors, queue connections, and model-specific overrides.

Released under the MIT License.