IMO 逐步过渡到 OOP 方法是绝对有效的。
你的问题:
是的,您可以单独使用 Eloquent。
这里是打包网站:https://packagist.org/packages/illuminate/database
将"illuminate/database": "5.0.*@dev" 添加到您的composer.json 并运行composer update。
现在你需要引导 Eloquent。 (https://github.com/illuminate/database)
以下内容是从 repo 的自述文件中复制的:
使用说明
首先,创建一个新的“Capsule”管理器实例。 Capsule 旨在尽可能轻松地配置库以在 Laravel 框架之外使用。
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
// Set the event dispatcher used by Eloquent models... (optional)
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Set the cache manager instance used by connections... (optional)
$capsule->setCacheManager(...);
// Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$capsule->bootEloquent();
一旦 Capsule 实例被注册。你可以这样使用它:
使用查询生成器
$users = Capsule::table('users')->where('votes', '>', 100)->get();
其他核心方法可以直接从 Capsule 以与 DB 门面相同的方式访问:
$results = Capsule::select('select * from users where id = ?', array(1));
使用架构生成器
Capsule::schema()->create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->timestamps();
});
使用 Eloquent ORM
class User extends Illuminate\Database\Eloquent\Model {}
$users = User::where('votes', '>', 1)->get();
有关使用此库提供的各种数据库工具的更多文档,请参阅 Laravel 框架文档。