How To Use Laravel’s Eloquent ORM with Zend Expressive

When working with Zend Expressive, if you were to choose a database layer for your application, I guess that the first two names to come to mind would be either Zend\Db or Doctrine. But what about Laravel’s ORM: Eloquent?

That’s right, Eloquent!

I’ve been getting to know Laravel a bit better recently, primarily for the comparison I wrote about it and Zend Expressive not that long ago.

The tutorial was exceptionally well received. So I thought I’d dig a bit further, and see if I could use Eloquent instead of my standard Zend\Db. It turns out, with a bit of work, you can do it!

In today’s tutorial, I’m going to step you through how to make it available, using a project based off of the Zend Expressive Skeleton Installer.


Actually, when it’s not used with Laravel, it’s not called Eloquent. Well in the repository’s not called that at least. It’s called Laravel-Database. Built on top of the illuminate/database package, in the words of the repository README, it:

Provides full Laravel database functionality for your non-Laravel projects adds Migration, Seeding and Artisan support to Illuminate Database.

To make it available, from the terminal, in the root of your project, run the following commands:

composer require laravel/database

When Composer’s done, you’re be ready to go.

Database Configuration

The first thing we need to do is to create a configuration to our database. To do that, in config/autoload create a new file, called In there, add the following configuration, adjusted for your database setup:

return [ 'eloquent' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'homestead', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', ] ];

You can see that we’re setting all of the relevant configuration options, required to connect to a local MySQL database.

Note: In case you’re wondering, I’m running my local copy of this application inside the Laravel Homestead VM. It’s a handy VM setup that provides services and a stock configuration where you can get started right away.

Making Eloquent Available to Zend Expressive

Next, we need to make Eloquent available, so that we can use it to connect to our MySQL database. To do that, we need to add the following lines to public/index.php, after require ‘vendor/autoload.php’;.

use Illuminate\Database\Capsule\Manager; /** @var \Interop\Container\ContainerInterface $container */ $container = require 'config/container.php'; $capsule = new Manager(); $capsule->addConnection($container->get('config')['eloquent']); $capsule->setAsGlobal(); $capsule->bootEloquent();

What this does is to make the Eloquent connection manager globally available, using the configuration which we provided in the last section.

Now, whether you think that having it globally available is a good thing or not, I’ll leave for another time. For right now, we can now move on and create a model class.

Creating a Database Model

Originally published at

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.