<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 在 Slim 中使用 Eloquent 你可以使用 [Eloquent](https://laravel.com/docs/5.1/eloquent) 這種數據庫 ORM 將你的 Slim 應用程序連接到數據庫。 ## 為你的應用程序添加 Eloquent ``` composer require illuminate/database "~5.1" ``` Figure 1: Add Eloquent to your application. ## 配置 Eloquent 在 Slim 的 setting 數組中添加數據庫設置項。 ``` <?php return [ 'settings' => [ // Slim Settings 'determineRouteBeforeAppMiddleware' => false, 'displayErrorDetails' => true, 'db' => [ 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'user', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ] ], ]; ``` Figure 2: Settings array. 在 `dependencies.php` 中,或者其他任意位置添加你的 Service Factories: ``` // Service factory for the ORM $container['db'] = function ($container) { $capsule = new \Illuminate\Database\Capsule\Manager; $capsule->addConnection($container['settings']['db']); $capsule->setAsGlobal(); $capsule->bootEloquent(); return $capsule; }; ``` Figure 3: Configure Eloquent. ## 傳遞數據表實例到控制器 ``` $container[App\WidgetController::class] = function ($c) { $view = $c->get('view'); $logger = $c->get('logger') $table = $c->get('db')->table('table_name'); return new \App\WidgetController($view, $logger, $table); }; ``` Figure 4: Pass table object into a controller. ## 從控制器中查詢數據表 ``` <?php namespace App; use Slim\Views\Twig; use Psr\Log\LoggerInterface; use Illuminate\Database\Query\Builder; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; class WidgetController { private $view; private $logger; protected $table; public function __construct( Twig $view, LoggerInterface $logger, Builder $table ) { $this->view = $view; $this->logger = $logger; $this->table = $table; } public function __invoke(Request $request, Response $response, $args) { $widgets = $this->table->get(); $this->view->render($response, 'app/index.twig', [ 'widgets' => $widgets ]); return $response; } } ``` Figure 5: Sample controller querying the table. ### 使用 where 查詢數據表 ``` ... $records = $this->table->where('name', 'like', '%foo%')->get(); ... ``` Figure 6: Query searching for names matching foo. ### 通過 id 查詢數據表 ``` ... $record = $this->table->find(1); ... ``` Figure 7: Selecting a row based on id. ## 了解更多 [Eloquent](https://laravel.com/docs/5.1/eloquent) 文檔
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看