IMO 逐步过渡到 OOP 方法是绝对有效的。
对于您的问题:
是的,您可以单独使用 Eloquent。
这是 packagist 网站: https://packagist.org/packages/illuminate/database 添加到你的并运行 .现在,您需要引导Eloquent。(https://github.com/illuminate/database"illuminate/database": "5.0.*@dev"
composer.json
composer update
)
以下内容是从存储库的自述文件中复制的:
使用说明
首先,创建一个新的“胶囊”管理器实例。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();
注册胶囊实例后。您可以像这样使用它:
使用查询生成器
$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();
});
使用雄辩的 ORM
class User extends Illuminate\Database\Eloquent\Model {}
$users = User::where('votes', '>', 1)->get();
有关使用此库提供的各种数据库工具的进一步文档,请参阅 Laravel 框架文档。