0

Laravel的Eloquent ORM怎样在其他框架里面单独使用?

lydia created at6 years ago view count: 2794
report
回复
0

安装依赖包

composer require illuminate/database

配置数据库

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();

使用

添加scheme

Capsule::schema()->create('users', function($table)
{
    $table->increments('id');
    $table->string('email')->unique();
    $table->timestamps();
});

使用query builder

$users = Capsule::table('users')->where('votes', '>', 100)->get();

使用Eloquent ORM

class User extends Illuminate\Database\Eloquent\Model {
}

$users = User::where('votes', '>', 1)->get();
6 years ago 回复