Connecting to IBM i Db2 with the Laravel PHP Framework

Laravel FrameworkLaravel has been inspiring loyalty among PHP developers for its documentation, community, and many built-in features.

Like most PHP frameworks, Laravel is cross-platform and can work with IBM i and Db2 data. This article shows how to take full advantage of Laravel’s built-in database components by installing an IBM i-specific extension to Laravel’s database connection class.

A new fork for a useful package

For years, PHP developers used the laravel-db2 package from cooper22 to connect to IBM i data. As the cooper22 package became stale and it slowed its acceptance of community updates, users stepped up. BWI Companies, in cooperation with Seiden Group, created a “fork” (a copy), dubbed it db2-driver, and is maintaining it actively.

What you can do with db2-driver

Once you configure access to your IBM i database, you can use several popular Laravel features with it, including Query Builder; Migrations; and Eloquent, Laravel’s object-relational mapper (ORM).

Requirements

PDO_ODBC: BWI Companies decided to simplify the new package by limiting support to PDO_ODBC, which works well on IBM i and can connect to IBM i from other platforms. Support for the legacy PDO_IBM and ibm_db2 extensions were not carried forward in this package, although Seiden Group continues to maintain and support PDO_IBM and ibm_db2 in its CommunityPlus+ PHP package. The ODBC driver supports library lists and other popular IBM i Db2 features.

PHP 8.1+: This package requires PHP 8.1 or higher.

How to install and configure db2_driver

The readme file is the best place to start, but we will summarize the steps here:

  • Install the package via composer inside your project directory:
  • Add a new connection in database.php, using the example settings in the readme as a guide. All supported ODBC connection string keywords are available there, including library list support.
    • Several database.php values, such as env('DB_HOST'), will be pulled from the .env file in the main Laravel folder.
    • Specify your default database connection name, which can be any string. Example:
      • 'default' => env('DB_CONNECTION', 'myIbmi'),
    • Add details in an array for the database connection. Example key name (paste in your database.php settings after this):
      • myIbmi' => [
  • Edit the .env file to add key database connection settings, such as password. The .env file is not considered “code” and by default won’t be committed to Git repositories. Example settings:
    • DB_CONNECTION=myIbmi # any name you want that matches main key in database.php
    • DB_HOST=127.0.0.1 # server/IP address
    • DB_DATABASE=*LOCAL # DSN name from odbc.ini or other ODBC DSN storage
    • DB_USERNAME= # IBM i user with rights to your objects
    • DB_PASSWORD= # IBM i password
    • Optional settings:
      • DB_SCHEMA=# if using a single IBM i Db2 default library
      • DB_PORT=50000 # if connecting from Linux, Windows, or non-i platform

Run a query and get data

Now you should be able to connect to your IBM i data using code like the following:

Laravel PHP and IBM i Db2 working together

Give the DB2 for IBM i Laravel Driver a try. It’s open source, accepting improvements and enhancements. I hope a community of IBM i PHP developers join us to contribute actively and help to make it even better.

Thank you to BWI Companies and all those who contributed to the original package.

As always, stay in touch and let us know if you would like help getting started or support to keep up your skills.

1 reply
  1. Néstor López
    Néstor López says:

    I’ve tried to connect to DB2 database but I get the following error:
    PDOException
    SQLSTATE[08004] SQLDriverConnect: 10061 [IBM][System i Access ODBC Driver]Communications link error. comm rc=10061 – CWBCO1049 – The IBM i server application has not started or the connection has been blocked by a firewall

    PHP 8.2.15
    Laravel 10.41.0

    What should I do?

    Have a nice day

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.