It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site. Our users don't see ads
  2. Or, Sign up if you don't have an account yet
  3. Or, disable your adblocker by doing this:
    • Click on the adblocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

Workouts with Slim 3:Database with PDO and Eloquent

By zooboole

Databases become a must for your applications every time you need to persist some data. People build most modern applications in this architecture. It's important you know how to handle database transaction with your Slim 3 code. One can ask himself how can we connect to our database, how can we use the database connection to query our tables?

As we saw in preview introductory parts Slim 3 is a great tool that you can use and build all types of web applications. The framework only handles HTTP requests and responses. Any other functionality needed must be added. Sometimes this makes some tasks not be standard. You will discover many people doing this in many other ways, the best is the one that suits your case the best.

Using PDO with Slim 3

Now let's look at how we can use PDO to interact with our database. As you know, the first option is always to inject a PDO instance to our container and then make use of it in our routes like following:

// Settings
$settings = [
    'settings' => [

        'pdo' => [
            'engine' => 'mysql',
            'host' => 'localhost',
            'database' => 'dbname',
            'username' => 'user',
            'password' => 'some_password',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',

            'options' => [
                    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                    PDO::ATTR_EMULATE_PREPARES   => true,
                ],
        ],

    ],
];

// Get container
$container = $app->getContainer();

// Inject a new instance of PDO in the container
$container['dbh'] = function($container) {

        $config = $container->get('settings')['pdo'];
        $dsn = "{$config['engine']}:host={$config['host']};dbname={$config['database']};charset={$config['charset']}";
        $username = $config['username'];
        $password = $config['password'];

        return new PDO($dsn, $username, $password, $config['options']);

};

This makes an instance of PDO available in our container and we can access it like following in our routes:

$app->get('/some/path', function($req, $res, $args) {

    $dbhandler = $this->dbh;
    $queryUsers = $dbhandler->prepare(" SELECT * FROM users LIMIT 100 ");

    try{
        $queryUsers->execute();
        $users = $queryUsers->fetchAll();

        return $users;

        // or you can send it to a template
        // $this->view->render($res, 'template.twig', $users);

    }catch(\PDOExeption $e)
    {
        // handle exception
    }

});

You are now ready to make use of PDO anywhere you want. The problem with this option is that you code could become cumbersome in time. You will have fetch the PDO instance in any single route. We could create an abstract class to handle that.

<?php

namespace App\Models;

Abstract class Model{

    public funcion __construct($req, $res, $arg)
    {
        $this->dbhandler = $this->dbh;
    }

}

Once that done, any [invokable]() class can extend the model class and access the PDO instance too.

<?php

namespace App\Controllers;

class user extends \App\Models\Model{

    // ...

    $this->dbhandler->prepare( " SELECT * FROM users" );

    // ...

}

Using Eloquent

This opens a new option which is to use Laravel Eloquent instead of raw PDO. Eloquent frees you from dealing with SQL queries by creating an abstraction layer between you and your databases. It allows you to access your database using a set of objects, with a simple API for storing and retrieving data. It also provides a query builder.

To use it in slim, of course we need to bring it in our application. You can do that by running this:

composer require illuminate/database

This time, instead of creating a main model, we'll be using Eloquent as our main model. Then when we create a new model, that model extends the eloquent. For example, a user model could be like this:

<?php
namespace App\Models;

use \Illuminate\Database\Eloquent\Model;

class user extends Model
{

    // ...

    protected $table = 'users';

    protected $fillable = [];

    // ...

}

And in a user controller we can make use of this model like following:

<?php

use App\Models\User;

final class UserController
{

    public function list(Request $request, Response $response, $args)
    {
        $users = user::all();
        // var_dump($users);

        $this->view->render($response, 'users.twig', $users);
    }

And we have the same possibilities. Moreover we can use Eloquent query builder and have clean queries.

But remember before that works you need to boot eloquent and make it globally available in your application.

$capsule = new \Illuminate\Database\Capsule\Manager();
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

If you like you can inject the instance of Eloquent into your container

$container['db'] = function ($container) use ($capsule){
    return $capsule;
};

The instance could be used in you routes/invokable like this:

$capsule::table('users')->all();

Eloquent is deep, I suggest you go to its official page to learn more of how it's used. Just know this is a great tool to use and interact with your database.

Conclusion

Here is the end of the last tutorial in this series on Slim 3. I hope these few tutorials can help you jump-start with Slim. I might make some other tutorials on Slim 3 but those ones are going to be specific. If you have any idea of next topic I should be talking about let me know.

If you think there is something I should talk about too, let me know.

If you liked this, please share it or comment. Thanks for reading.

Last updated 2024-01-11 UTC