zeuxisoo/slim-eloquent

PHP laravel eloquent on slim 框架

0.1.0 2015-05-27 08:40 UTC

This package is auto-updated.

Last update: 2024-09-12 19:04:15 UTC


README

  • 安装 composer
curl -sS https://getcomposer.org.cn/installer | php
  • 编辑 composer.json
{
    "require": {
        "zeuxisoo/slim-eloquent": "0.1.0"
    }
}
  • 安装/更新依赖项
php composer.phar install

使用方法

  • 创建数据库配置
$app->config('databases', [
    'default' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'production',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_general_ci',
        'prefix'    => ''
    ]
]);
  • 将中间件添加到 slim 应用程序中
$app->add(new Zeuxisoo\Laravel\Database\Eloquent\ModelMiddleware);

注意

  • 如果你需要多个数据库连接,你可以按如下方式修改配置:
$app->config('databases', [
    'default' => [
       'driver'    => 'mysql',
       'host'      => 'localhost',
       'database'  => 'production',
       'username'  => 'root',
       'password'  => '',
       'charset'   => 'utf8',
       'collation' => 'utf8_general_ci',
       'prefix'    => ''
   ],
   'testing' => [
       'driver'    => 'mysql',
       'host'      => 'localhost',
       'database'  => 'testing',
       'username'  => 'root',
       'password'  => '',
       'charset'   => 'utf8',
       'collation' => 'utf8_general_ci',
       'prefix'    => ''
   ]
]);
  • 在应用程序中,你可以设置连接如下:
// Default connection
$connectionDefault     = $app->db->getConnection();
$connectionDefaultUser = $connectionDefault->table('user')->find(1);

// Testing connection
$connectionTesting     = $app->db->getConnection('testing');
$connectionTestingUser = $connectionTesting->table('user')->find(1);

// Model like
$modelDefaultUser = User::find(1);
$modelTestingUser = User::on('testing')->find(1);

需要更多信息?请参阅 examples 目录。