I started refactoring a Laravel app that has most of its logic in controllers, into services. I do not plan on using the repository pattern.
When using a model in a service class which is the better way?
-
Importing the model and using it directly eg.:
User::all()
-
Injecting it in the constructor of the service and using it like this:
$this->userModel->all()
>Solution :
Both methods are valid, but in many cases, injecting the model provides a good balance of flexibility and maintainability, especially as your application grows.
Ultimately, the choice depends on your specific needs and the complexity of your application. If you’re unsure, dependency injection can offer more long-term benefits.