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)
Eloquent ORM is not really a good choice when working with Dependency Injection, as it was developed with the Laravel Facade pattern in mind.
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.
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.
i really like the eloquent query builder⌠and unfortunately havent been able to find an alternative 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->prepare($query);
$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',
'i',
[$userId],
User::class
);
$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 ?