I'm in the process of building an endpoint system in PHP using Slim and Eloquent, as outlined here. When running it in my local dev, the code below fails with what appears to be a Fatal Error based on what the methods are expecting
// Load Eloquent
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory();
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
The actual error is:
[Wed Aug 13 10:31:44 2014] PHP Catchable fatal error: Argument 1 passed to
Illuminate\Database\Connectors\ConnectionFactory::__construct() must be an instance
of Illuminate\Container\Container, none given, called in
/Users/outsider/application/index.php on line 22 and defined in
/Users/outsider/application/vendor/illuminate/database/Illuminate/Database/Connectors/ConnectionFactory.php on line 25
There's not a whole lot of guidestones in the docs about this. Any ideas on the possible cause?
Thanks to Manolo for pointing out what I was missing. I needed to declare a Container and initialize it:
$container = new Illuminate\Container\Container;
$connFactory = new \Illuminate\Database\Connectors\ConnectionFactory($container);
$conn = $connFactory->make($settings);
$resolver = new \Illuminate\Database\ConnectionResolver();
$resolver->addConnection('default', $conn);
$resolver->setDefaultConnection('default');
\Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
There is an easier way to use Eloquent outside Laravel. You can use Capsule:
/* Setup Eloquent. */
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule = new Capsule;
$capsule->addConnection([
"driver" => "mysql",
"host" => "localhost",
"database" => "example",
"username" => "root",
"password" => "t00r",
"charset" => "utf8",
"collation" => "utf8_general_ci",
"prefix" => ""
]);
$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->bootEloquent();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With