前言:PHP 版本要求 >= 5.4,这是 Eloquent 的最低要求。
- 在 composer.json里面添加"illuminate/database": "*"依赖,并且执行composer update
- 在 application/helpers里添加一个 helper, 名为eloquent_helper.php,
 内容如下代码片段①。
- 在 config/autoload.php中的$autoload['helper']中添加 eloquent,效果如下:$autoload['helper'] = array('view', 'eloquent');
- 将 config/config.php中的$config['composer_autoload']配置修改为:$config['composer_autoload'] = './vendor/autoload.php';
- 将 application/config/database.php数据库连接配置正确。
代码片段①
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Illuminate\Database\Capsule\Manager as Capsule;
// require the ci database config
require_once APPPATH.'config/database.php';
// Eloquent ORM
$capsule = new Capsule;
$capsule->addConnection(
    array(
        'driver'    => 'mysql',
        'host'      => $db['default']['hostname'],
        'database'  => $db['default']['database'],
        'username'  => $db['default']['username'],
        'password'  => $db['default']['password'],
        'charset'   => $db['default']['char_set'],
        'collation' => $db['default']['dbcollat'],
        'prefix'    => $db['default']['dbprefix']
    )
);
//boot Eloquent
$capsule->bootEloquent();
一个Controller和Model示例:
application/models/Typecho_contents.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Illuminate\Database\Eloquent\Model as Eloquent;
class Typecho_contents extends Eloquent
{
    protected $table = 'typecho_contents';
    protected $primaryKey = 'cid';
    public $timestamps = false;
}
application/controllers/Blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
    public function index()
    {
        $this->load->model('typecho_contents');
        $example = $this->typecho_contents->findOrFail(1);
        dd($example->toArray(), Typecho_contents::all());
    }
}
