You need to do a few steps such as adding the connection settings in the file settings.php from folder app as follows:<?php
declare(strict_types=1);
use DI\ContainerBuilder;
use Monolog\Logger;
return function (ContainerBuilder $containerBuilder) {
// Global Settings Object
$containerBuilder->addDefinitions([
'settings' => [
'displayErrorDetails' => true, // Should be set to false in production
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout'
: DIR . '/../logs/app.log',
'level' => Logger::DEBUG,
],
'db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db',
'username' => 'root',
'password' => 'senha',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
],
]);
};
after this configuration inside the folder src create a folder Models (src\Models) placing your Models and a connection configuration that will be used in the route file, for example:Create the file so you can climb the settings Eloquent:<?php namespace App\Models;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
final class Bootstrap
{
public static function load($container)
{
$settings = $container->get('settings');
$capsule = new Capsule();
$capsule->addConnection($settings['db']);
$capsule->setEventDispatcher(new Dispatcher(new Container));
$capsule->setAsGlobal();
$capsule->bootEloquent();
}
}
and as an example Model with the name User (only to test):<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent
{
protected $table = 'users';
protected $fillable = ['name','email','password'];
protected $primaryKey = 'id';
protected $dates = ['created_at', 'updated_at'];
}
To finish between inside app\routes.php and just below the line: $container = $app->getContainer(); write the line Bootstrap::load($container); which is responsible for climbing the settings of Eloquent:<?php
declare(strict_types=1);
use App\Application\Actions\User\ListUsersAction;
use App\Application\Actions\User\ViewUserAction;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
use App\Models\Bootstrap;
use App\Models\User;
return function (App $app) {
$container = $app->getContainer();
Bootstrap::load($container);
$app->get('/', function (Request $request, Response $response) {
$response->getbody()->write(json_encode(User::all()->toArray()));
return $response;
});
$app->group('/users', function (Group $group) use ($container) {
$group->get('', ListUsersAction::class);
$group->get('/{id}', ViewUserAction::class);
});
};
the above example has the line of Model Eloquent:$response->getbody()->write(json_encode(User::all()->toArray()));
that will print the data from the table users where Eloquent was set up.Observations:Packages that should be installed for this example to work:"doctrine/dbal""illuminate/database""illuminate/events""illuminate/filesystem""illuminate/pagination""monolog/monolog"