Illuminate Database Capsule Connection Manager

Published on by Safeer

I’ve covered Dan Horrigans Illuminate Database Capsule Package in previous blog posts, it has since been merged into the the core Illuminate Database package. This means you no longer need to rely on an external package to simplify Database connection 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();