Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Eloquent ORM from Laravel 4 outside of Laravel

I created a folder 'eloquent' to start testing/learning the component, and my composer.json file is:

{
  "require": {
    "php": ">=5.3.0",
    "illuminate/database": "4.0.*"
  }
}

Below is my test.php file, with credentials removed. It works great, until I add ->remember(10) into the command. I'd like to look into adding the Illuminate Cache next then, if that's what's needed to start using ->remember(). Is anyone aware of any blog posts or tutorials on doing something like this?

<?php
/**
 * Testing Laravel's Eloquent ORM
 * @see https://github.com/illuminate/database
 * @see http://laravel.com/docs/database
 */
require 'vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

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

//$capsule->bootEloquent();
$capsule->setAsGlobal();

$name = Capsule::table('user')->where('id', 123 )->remember(10)->get();

var_dump( $name );

// PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Class cache does not exist'

I'm not sure what the next step is to get ->remember() working. I tried adding illuminate/cache to the composer.json file and updated. I wasn't sure how to use it with Eloquent, outside of Laravel.

like image 878
Poe Avatar asked Jan 14 '23 03:01

Poe


2 Answers

saff33r is right, but just to help someone like me that need "file" cache this is how to:
in your composer.json

    "illuminate/cache": "4.0.*",
    "illuminate/filesystem": "4.0.*",

in your boot file:

    use \Illuminate\Database\Capsule\Manager as Capsule;
    use \Illuminate\Cache\CacheManager as CacheManager;
    use \Illuminate\Filesystem\Filesystem as Filesystem;
    ...

    $container = $capsule->getContainer();
    $container['config']['cache.driver'] = 'file';
    $container['config']['cache.path'] = __DIR__ . '/uploads/cache/eloquent';
    $container['config']['cache.connection'] = null;
    $container['files'] = new Filesystem();

    $cacheManager = new CacheManager($container);
    $capsule->setCacheManager($cacheManager);
    $capsule->bootEloquent();
like image 197
Felice Ostuni Avatar answered Jan 17 '23 18:01

Felice Ostuni


It should already be pulling in "illuminate/cache", look in vendor and you should see it there.

You need to setup the cache manager, then pass that through by calling

$capsule->setCacheManager(CacheManager $cache);

I've not looked under the hood for details on how to do this but hopefully this will be enough details to get you going forward.

Edit:

Here's what you need to add to get it working:

use Illuminate\Cache\CacheManager as CacheManager;

$container = $capsule->getContainer();

$container->offsetGet('config')->offsetSet('cache.driver', 'array');

$cacheManager = new CacheManager($container);

$capsule->setCacheManager($cacheManager);

Obviously feel free to change the Cache Driver used but keep in mind that changing the Cache Driver will require adding the extra required settings.

like image 21
Safeer Avatar answered Jan 17 '23 16:01

Safeer