Okta Anang Diantama
Okta Anang Diantama

Reputation: 51

using eloquent on Codeigniter 3

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

Upvotes: 5

Views: 10094

Answers (3)

Naitoreven
Naitoreven

Reputation: 101

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.

Upvotes: 0

Fachri Hawari
Fachri Hawari

Reputation: 11

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

$config['composer_autoload'] = 'path/to/vendor/autoload.php';

Upvotes: 1

Evis
Evis

Reputation: 561

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.

Upvotes: 1

Related Questions