idl-bnl-dev/db2-driver
A Laravel database driver for IBM DB2, adding DB2 connectivity to Laravel's query builder, Eloquent, and migration tooling. Configure a DB2 connection and use familiar Laravel database APIs with your DB2 databases.
idl-bnl-dev/db2-driver adds IBM Db2 connectivity to Laravel's database layer, so the fastest path is to treat it as a normal Laravel connection rather than a separate API.
ibm_db2 or pdo_odbc, depending on how the package is configured — check the README before assuming). The DB2 CLI/ODBC driver must be installed on the host and reachable by the PHP process, not just by your db2 command-line client.config/database.php using the driver name the package registers (commonly db2), with host, port, database, username, password, and any protocol/schema options from the README. Keep credentials in .env.config/ defaults, then tests/ — the 2.1.0 release added live database tests, which are the most reliable documentation of supported config keys and expected behaviour.DB::connection('db2')->select(...) queries and one small Eloquent model. This validates the driver, credentials, and identifier handling before you commit to it for writes.Start with reads. Confirm the connection works end-to-end (including character encoding and date/time values) before migrating any write paths onto it.
Keep Db2 as a non-default connection. In most Laravel apps Db2 is a legacy or reporting system alongside MySQL/Postgres. Define a named connection and access it explicitly:
DB::connection('db2')->table('ORDERS')->where('STATUS', 'OPEN')->get();
Use the new read-only mode for reporting paths (2.1.0+). Configure a dedicated connection flagged read-only so accidental writes fail fast rather than corrupting a production Db2 system. The release adds a custom config key for this — copy the exact key and value from the README/tests rather than guessing. This pairs well with a separate DB user that only has SELECT grants; treat the driver flag and the database grants as two independent layers.
Wrap Db2 access in a repository, not scattered in controllers. Db2 SQL is often hand-tuned (fetch-first, WITH UR, schema qualification), so isolate raw statements behind a small repository class and return Eloquent models or DTOs. That keeps DB2-specific syntax out of your domain code and makes it swappable for a test double.
Use raw queries where the grammar doesn't fit. Simple where/join/paginate usually works, but window functions, FETCH FIRST n ROWS ONLY, isolation clauses, and complex MERGE statements are better expressed as DB::connection('db2')->select($sql, $bindings). Use bindings — never interpolate.
Schema-qualify model tables. Set protected $table = 'MYSCHEMA.ORDERS'; or set the connection's schema option. Don't rely on the connecting user's default schema being what you expect.
Testing pattern. The 2.1.0 live tests confirm the package is validated against a real Db2 instance, which is the right model for your own suite too: gate integration tests behind an env var (e.g. DB2_TESTING=true) so CI without a Db2 service skips them, and use SQLite/array connections for the rest. Never assume SQLite parity — it will not catch DB2 grammar or type issues.
Migrations. Keep Db2 schema under explicit control. If you run migrations against Db2, verify each column type and index statement on a scratch database first; otherwise manage Db2 DDL out-of-band and reserve Laravel migrations for your primary database.
->where('status', ...) may fail or silently mismatch depending on how the grammar quotes identifiers. Match the catalog exactly (STATUS), and be deliberate about quoting mixed-case names.insert() on the read-only connection actually throws rather than being silently ignored.lastInsertId and sequences. Auto-increment retrieval is driver- and Db2-version-dependent. Prefer explicit sequence NEXTVAL retrieval or INSERT ... RETURNING-style patterns, and verify with a test rather than assuming create() returns a populated id.WITH UR on the query, and keep transactions short. Nested transactions/savepoints may not behave like other drivers.offset/limit translation is a common source of bugs in non-standard grammars. Test paginate() on a real Db2 instance, especially with orderBy on non-unique columns.DECIMAL, TIMESTAMP, DATE, and CHAR (space-padded) values come back. CHAR padding in particular breaks naive string comparisons in PHP. Cast explicitly in models.DB::connection('db2')->enableQueryLog() and DB::listen() to capture the final SQL and bindings; also check the Db2 CLI trace (DB2CLIINIPATH/db2cli.ini tracing) when the failure is below Laravel — connection resets and encoding errors often surface as generic PDO exceptions.How can I help you explore Laravel packages today?