Using Eloquent with Slim

Adding Eloquent to your application
The first step is importing the Eloquent ORM into your project using composer.

composer require illuminate/database

Configure Eloquent
On your bootstrap file


use DI\Bridge\Slim\Bridge;
use DI\Container;
use Illuminate\Database\Capsule\Manager;


require __DIR__ . '/../vendor/autoload.php';

$app = Bridge::create(new Container);

$config = [
     'driver' => 'mysql',
     'host' => '',
     'database' => 'dbname',
     'username' => 'dbuser',
     'password' => 'dbpass',
     'charset' => 'utf8',
     'collation' => 'utf8_unicode_ci',
     'prefix' => ''

$manager = new Manager;

Or you can split it to a different file and require it on your bootstrap.

Create a model that represent your table name for example users table.


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
  protected $table = 'users'; // table name

Query from a controller


use App\Models\User;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class UserController
    public function getUsers(Request $request, Response $response): Response
        $users = User::all();
        return $response->withJson($users); // or use twig to return the results

Is this something new in regards to Slim 4 + Eloquent ORM ?