I’m building a small Slim Framework application at New Media Campaigns. Because this application is for internal use, I figured I’d learn something new while building it. Even though the database schema is pretty simple, I’ve heard great things about Laravel — a full-stack framework from Taylor Otwell — and how many of its components are available as separate Composer packages on Packagist.
So I decided to use Laravel’s Eloquent ORM with my Slim Framework application. Several of my readers asked how I did this. So here goes…
Here’s how I’m organizing my application. The virtual host document root is the
First, prepare the
composer.json file so it will pull down and install the Slim Framework and the Eloquent ORM. The composer.json file should look like this:
When this is done, run composer install to install the application dependencies.
Bootstrap The Eloquent ORM
Next, I tell Composer to autoload the application’s dependencies by requiring Composer’s
And now I bootstrap the Eloquent ORM and pass it my database connection information (be sure you add your own username, password, and database name).
Now that the Eloquent ORM is bootstrapped, I can create and use models that extend the Eloquent abstract model. This example assumes your database contains a table named books with columns title and author.
Include this model file into index.php and use your Eloquent ORM models in your Slim application routes: