5

After release of Codeigniter 3 and from this tutorial I have some problem

I want to use Eloquent and do this steps

  1. Install Composer
  2. Do install dependencies using this json file

     {
          "require": {
          "illuminate/database": "4.2.6"
     },
    
    "autoload": {
    "classmap": [
     "application/core",
    "application/models",
    "application/libraries"
    ]
    },
    
    "config": {
    "vendor-dir": "vendor/"
    }
    }
    
  3. Successful installation

  4. Make some configuration below:

a. update libraries on autoload.php => $autoload['libraries'] = array('database');

b. update config.php => $config['composer_autoload'] = TRUE;

c. update database.php

  $active_group = 'default';
  $query_builder = TRUE;

  $db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'ci3',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
  );
  use Illuminate\Database\Capsule\Manager as Capsule;

  $capsule = new Capsule;

  $capsule->addConnection(array(
      'driver' => in_array($db['default']['dbdriver'], array('mysql', 'mysqli')) ? 'mysql' : $db['default']['dbdriver'],
      'host' => $db['default']['hostname'],
      'database' => $db['default']['database'],
      'username' => $db['default']['username'],
      'password' => $db['default']['password'],
      'charset' => 'utf8',
      'collation' => 'utf8_unicode_ci',
      'prefix' => $db['default']['dbprefix'],
    )
  );

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

  $events = new Illuminate\Events\Dispatcher;
  $events->listen('illuminate.query', function($query, $bindings, $time, $name) {

    // Format binding data for sql insertion

    foreach ($bindings as $i => $binding) {
      if ($binding instanceof \DateTime)  {
        $bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
      } else if (is_string($binding)) {
        $bindings[$i] = "'$binding'";
      }
    }

    // Insert bindings into query
    $query = str_replace(array('%', '?'), array('%%', '%s'), $query);
    $query = vsprintf($query, $bindings);

    // Add it into CodeIgniter
    $db =& get_instance()->db;
    $db->query_times[] = $time;
    $db->queries[] = $query;
  });

  $capsule->setEventDispatcher($events);

d. make a database called ci3 and table users instead of it

This is my model

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

use \Illuminate\Database\Eloquent\Model as Eloquent;

class Users extends Eloquent {
    protected $table = "users";
}

and my controller

    <?php

  if (!defined('BASEPATH')) exit('No direct script access allowed');

  class User extends CI_Controller {

      public function __construct() {
          parent::__construct();
          $this->load->model('users');
      }

      public function index($page = 1) {
          $users = Users::all();
          foreach ($users as $user) {
              echo '<li>' . $user->username . '</li>';
              echo '<li>' . $user->password . '</li>';
              echo '<li>' . $user->email . '</li>';
          }
          echo '</ul>';
      }

  }

and this is my error report

Fatal error: Class 'Illuminate\Database\Capsule\Manager' not found in C:\xampp\htdocs\ci3\application\config\database.php on line 88

A PHP Error was encountered

Severity: Error

Message: Class 'Illuminate\Database\Capsule\Manager' not found

Filename: config/database.php

Line Number: 88

Backtrace:

I have no idea how to put capsule class

Naing Lin Aung
  • 3,373
  • 4
  • 31
  • 48
  • Did you require the `vendor/autoload.php` file somewhere that you are not showing as part of your bootstrapping process? You will need to use that in order to get the autoloading for the stuff you have installed via Composer, ie. Eloquent and its dependencies. – prodigitalson Apr 03 '15 at 03:39
  • At this point it would be much better to use laravel directly – dynamic May 31 '15 at 09:24
  • Sweet, this helped me run Eloquent with CodeIgniter 2.2 and Illuminate/database 5.0.4 and Illuminate/events 5.0.4. Thanks. – Logus Graphics Jul 23 '15 at 23:59
  • The link to the tutorial is dead – Blip Jul 10 '20 at 06:30

3 Answers3

1

Here is the problem... you miss the 'events' library from laravel, add the below line to your composer.json:

"illuminate/events": "5.0.28"

In my case, I used Laravel 5, so, my composer.json looks like:

{
        "require": {
                "illuminate/database": "5.0.28",
                "illuminate/events": "5.0.28"
        } 
}

Please check how I implemented here.

Evis
  • 571
  • 8
  • 22
1

What you have changed the file application/config.php ?

$config['composer_autoload'] = 'path/to/vendor/autoload.php';
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

I solved it by using:

composer install

Looks like composer update was not enough and got the same error.

I still don't know how to link to possible duplicate question (Message: Class 'Illuminate\Database\Capsule\Manager' not found in Codeigniter3.1), hope this help someone facing the same error.

Naitoreven
  • 63
  • 7