How to use Illuminate Database Query Builder & Eloquent in other framework with PHP 5.3.x

ghiscoding picture ghiscoding · Dec 18, 2013 · Viewed 9.3k times · Source

I did a couple of projects with Laravel in the past but now I need something very light for a project and went to use Slim, it works great for what I need and I want the great Eloquent ORM and Query Builder of Laravel, can't go without it now :) Now I manage to get it all working with composer, using the information that Taylor displayed on his GitHub, copied his piece of code

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => '',
    'username'  => '',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
]);

// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));

// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);

// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();

// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();

That works perfectly good on my local dev (PHP 5.4.x) but when I put it on my server PHP 5.3.x, it just doesn't work anymore :( Now I see 1 problem being that we can't use anonymous array like this [] but instead should be written the old way of array(), that is inside the addConnection(array($settings)) great, now a little further...but then after it seems to crash inside $capsule->setEventDispatcher() and I don't have logs on my server (I only found through var_dump() here and there), it's just a NAS and I don't want to even think of spending a few hours just to find out how to enable it. But what's funny is that I had a Laravel 4 project working with it... so why just building a part of it Illuminate\Database doesn't work then? I also found another piece of code to make Eloquent ORM work in PHP 5.3.x

$settings = array(
    'driver' => '',
    'host' => '127.0.0.1',
    'database' => '',
    'username' => '',
    'password' => '',
    'charset'   => "utf8",
    'collation' => 'utf8_general_ci',
    'prefix' => ''
);


// Bootstrap Eloquent ORM
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory(new Illuminate\Container\Container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);

but if I use this piece of code, which is good with Models by the way. I need to use $conn->table('...')... instead of the Facade simple way of DB::table(....) which is what I want, why is it important you would say? Well what if I want to convert to Laravel in the future... I would have to change all of the $conn-> to DB:: so I would rather do it right the first time. If someone knows how to create Facade on the 2nd piece of code, I would be happy too... Thanks for any help.

Answer

Paul Dragoonis picture Paul Dragoonis · Dec 18, 2013

Look at the Laravel datasource connector in PPI 2.1

https://github.com/ppi/framework/tree/2.1/PPI/DataSource/Connection