Using Eloquent with Slim
Figure 1: Add Eloquent to your application.
return [
'settings' => [
'determineRouteBeforeAppMiddleware' => false,
'displayErrorDetails' => true,
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]
],
];
Figure 2: Settings array.
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.
Figure 5: Sample controller querying the table.
...
Figure 6: Query searching for names matching foo.
Query the table by id
Figure 7: Selecting a row based on id.