Slim Php, Eloquent and DB facade

Lately I have been using the Slim Php microframework along with some other additional components to develop some personal projects. So far the experience has been pleasant and straightforward but not without the typical problems that one has to face when learning new frameworks.

I had a small problem regarding the execution of custom SQL queries (raw queries) instead of model methods. Usually you can use a query builder whose use depends on the ORM. In this particular case I am going to talk about Eloquent that is used in the Laravel framework and that I managed to integrate in my slim project.

To install it, according to the explanation given in the  slim blog, it must be added as a dependency to the composer.json file and then create a new instance in the application’s bootstrap. It was also recommended to use a “capsule” component that acts as a wrapper to ease the integration outside Laravel. This looked more or less like this (some code was omitted for simplicity’s sake)

 

{
    "require": {
        "slim/slim": "2.*",
        "illuminate/database": "*",
        "dhorrigan/capsule": "*"
    }
}

The new instance was created like this:

require 'vendor/autoload.php';

$app = new SlimSlim();

// Make a new connection
$app->db = CapsuleDatabaseConnection::make('default', [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'port'      => 3306,
    'database'  => 'example',
    'username'  => 'user',
    'password'  => 'password',
    'prefix'    => '',
    'charset'   => "utf8",
    'collation' => "utf8_unicode_ci",
], true);

$app->get('/users', function () use ($app) {

    $users = User::all();

    $res = $app->response();
    $res['Content-Type'] = 'application/json';
    $res->body($users);
});

$app->run();

Then, according to the official documentation,  the DB facade can be called by using it

use DB;

After that we can use methods such as insert, delete, select, update, raw, etc. Continuing with our example:

$users = DB::select('select * from users where active = ?', [1]);

Here is where the problems began to appear. First of all the capsule component (“dhorrigan/capsule”: “*”) is no longer available, but the good thing is that now, it’s part of the Eloquent core and because of this we don’t need to add it in our composer.json

But the worst one occurred when trying to use the DB facade. I got errors like:  ‘ErrorException’ with message ‘Non-static method Illuminate\Database\Query\Builder::select() should not be called statically, assuming $this from incompatible context’.

The solution for Slim Php, Eloquent and DB facade

In order to set up everything correctly, I had to make some modifications to composer.json as well to the process of creating a new Eloquent instance.

First things first. We remove the capsule component because it’s no longer needed.

{
    "require": {
        "slim/slim": "*",
        "illuminate/database": "*"
    }
}

Then we include Eloquent in our app’s bootstrap.

$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection(array(
    'driver'   => 'mysql',
    'host'     => 'localhost',
    'port'     => '3306',
    'username'     => 'user',
    'password' => 'password',
    'database'     => 'mydb',
    "charset"   => "utf8",
    'collation' => 'utf8_general_ci',
););
$capsule->setEventDispatcher(new Illuminate\Events\Dispatcher(new Illuminate\Container\Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();

Finally we can use the DB facade in a slightly different way. For example in the following model:

use Illuminate\Database\Capsule\Manager as DB;
class User extends \Illuminate\Database\Eloquent\Model
{
    protected $table = 'user';
  
    public function getUser($id){
        $query = "SELECT * FROM user"; 
        return DB::select(DB::raw($query));		
  }

}

The query is very simple but it works as a basic example to illustrate how this works.