在沒有導入 Laravel 的情況下,也可以獨立使用 Eloquent ORM

Composer 安裝

{
    "require": {
        "illuminate/database": "*"
    }
}
composer install

Init Eloquent
database.php

require '../vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;

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

$capsule->bootEloquent();

Use

require_once 'database.php';

// Create Model
class Member extends Illuminate\Database\Eloquent\Model {
    protected $table = 'member';
    protected $primaryKey = 'nacid';
    public $timestamps = false;
}

$member = Member::find('johnson');
print_r($member->toArray());
Categories: Laravel