php – 在Codeigniter 3上使用eloquent

在Codeigniter 3发布之后,从这个
tutorial发布

我有一些问题

我想使用Eloquent并执行此步骤

>安装Composer
>使用此json文件安装依赖项

 {
      "require": {
      "illuminate/database": "4.2.6"
 },

"autoload": {
"classmap": [
 "application/core",
"application/models",
"application/libraries"
]
},

"config": {
"vendor-dir": "vendor/"
}
}

>成功安装
>在下面进行一些配置:

一个.更新autoload.php =>上的库$autoload [‘libraries’] = array(‘database’);

湾update config.php => $config [‘composer_autoload’] = TRUE;

C.更新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.创建一个名为ci3和表用户的数据库而不是它

这是我的模特

<?php

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

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

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

和我的控制器

    <?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>';
      }

  }

这是我的错误报告

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:

我不知道怎么放胶囊类

最佳答案 这是问题…你错过了laravel的’events’库,将以下行添加到你的composer.json:

“照亮/事件”:“5.0.28”

就我而言,我使用的是Laravel 5,所以,我的composer.json看起来像:

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

请检查我是如何实施here的.

点赞