Use query builder for slim


I have always use PDO only for access to my database and I try to use Laravel query builder and Eloquent ORM, for simplify my code.

My first question is about Eloquent model, in my container DI definition I have put the Manager capsule like this:

Manager::class => function(ContainerInterface $container)
        $settings = $container->get(Settings::class);
        $capsule = new Manager();
        return $capsule;

And In my controller I can use the query builder with this and it’s work well:

public function __construct(Manager $db)

But If in my controller I call only the model without the $db it’s not working:

public function __construct(LoginsModel $loginsModel)

First question: I need to call both, so I don’t know how I can call only the model and then the “Manager $db” automaticaly instance the model ? (I have try to create a constructor inside the model that instance the db but it’s not working, I think Eloquent doesn’t permit this).

Second question: I have a lots of complex queries (with multiples inner join / left, subquery, etc …) that I use multiples in multiple controller. For good practice it’s better to create a class / function getMyComplexQuery() inside a “Model” folder (or Repository / Services) ?

Third question: Which query builder / orm, you recommand to use for simplify the code of my sql queries (or maybe you recommand just PDO) ? (I have try with Lavarel but I can change)

Thanks for your help

1 Like
  1. Eloquent ORM is not really a good choice when working with Dependency Injection, as it was developed with the Laravel Facade pattern in mind.

  2. It’s a good practice to move complex queries into a separate class and method. This helps to keep your controllers lean and focused on handling requests and responses. You can create Repository classes and put your complex queries in there. For example, you can create a UserRepository class and define methods like findUsersWithOrders() or findUsersWithComments() that encapsulate complex queries. Then, you can inject this UserRepository into your Service and call the methods when you need them.

  3. I would not recommend using an ORM or PDO. I would recommend the use of a QueryBuilder, for various reasons, which I explain in detail in this video: Beyond ORM - A guide for advanced database queries - YouTube


Subscribed! Thats is a great video! I really appreciate your tone of voice and way of explaining. The only thing I maybe missed was a quick overview how to use the recommended libraries but I can imagine those can be entire videos on their own.

1 Like

Thanks @reinier for your feedback :slight_smile:

… but I can imagine those can be entire videos on their own.

Yes, this is another topic and already on my YouTube todo list.

1 Like

i really like the eloquent query builder… and unfortunately havent been able to find an alternative :frowning: i fell into the eloquent trap and the “magic” of the db capsule thing… arrggghhh… in my next version of my skeleton im going back to pure objects (non eloquent) for models and only using eloquent as my data layer)

Im using my own custom super simple query ‘builder’ right now.

internally it just wraps something like this:

        $statement = $this->mysqli->stmt_init();
        $statement->bind_param($bindParams, ...$params);
        $statementExecuted = $statement->execute();

        // For queries like insert and delete, we only want to know execution success
        if (!$returnResult) return $statementExecuted;

        return $statement->get_result();

It automatically converts select query and update query results to an object.

Use it something like this:

$user = $database->selectQuery(
    'SELECT * FROM users WHERE id = ? LIMIT 1',

$userId = $user->id

Thanks Odan for your answer and your video is perfect, I am completly agree with you so I will just use query builder without ORM (it was just a try because I see a lots of people use this, but your video confirm what I thought about the ORM).
You have also win a new subscriber on your youtube channel!

Then, you can inject this UserRepository into your Service and call the methods when you need them.

Q1: When you say to inject UserRepository into Service, why not directly in the Container ? The Service looks like what ? (maybe multiple injected Repository into LoginService like UserRepository LevelRepository, and then for example on my LoginController I just inject the LoginService ?)

Q2: I have look on the different query builder from your youtube links, but I can’t decide for one, all have their own advantage. Personnaly you use which query builder ?

Thanks for your helps :slight_smile:

I think he means that you use dependency injection with the container to automatically inject the userrepository into the service

Yes maybe it’s this, thanks