Skip to content

Illuminate Database Capsule: Eloquent Outside Laravel 4

Published on by

Configure a standalone database connection and boot Eloquent outside Laravel 4 using the original Illuminate Database Capsule connection manager.

I’ve covered Dan Horrigan’s Illuminate Database Capsule package in the CodeIgniter and Eloquent integration tutorial. It has since been merged into the core Illuminate Database package. This means you no longer need to rely on an external package to simplify database connections when using Illuminate Database in a non-Laravel 4 project.

Here’s a quick run through on how to setup a Database connection using the newly merged Illuminate Database Capsule. Start by making sure that you have the Illuminate Database Package in your composer.json.

"illuminate/database": "4.0.*"

Then run Composer update to fetch the latest changes. This will ensure that the new code changes are pulled in. Once this has finished, you can setup a connection to the Database by using the following code:

// Create a Illuminate\Database\Capsule instance
$capsule = new Illuminate\Database\Capsule(array(
    'fetch' => PDO::FETCH_CLASS, // How to return the results
    'default' => 'mysql',                        // Settings to use when connecting to the Database
    'connections' => array(                      // Database settings
        'mysql' => array(
            'driver'    => '',
            'host'      => '',
            'database'  => '',
            'username'  => '',
            'password'  => '',
            'charset'   => '',
            'collation' => '',
            'prefix'    => '',
        )
    )
));

// Prepare Eloquent ORM for use
$capsule->bootEloquent();

// Grab a Database Instance
$connection = $capsule->connection();