laravel using `new` keyword for models in controllers

I know this might be the point of view question which has no place on SO, but I am really trying to get experienced developer’s thoughts.

In my controllers, I don’t want to use models directly. So I use services injected in a controller’s constructor.

In the services, we have 2 ways to initialize models.

  • inject them in a service’s constructor.
  • use the models directly in a service’s functions, like new userModel().

Option 1 explanation: I can see the advantage of the first option, due to the fact that I can test service’s functions without touching database models, since I will mock the models which gives me the option that I separately test service’s functions without actually doing anything on models/databases.

Option 2 explanation: for the second option, all I can see is a disadvantage due to the fact that in order to test a service’s function, I also have to know in advance what models(database) returns . These kind of tests are also necessary, but we also need to test service’s functions without touching databases.

What are your thoughts on this ? I’d really appreciate it if you could give your thoughts on this even though this question mightn’t deserve to be here, since getting an answer takes lots of struggles without using stackoverflow.

from Newest questions tagged laravel-5 – Stack Overflow

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.