7
What is the real usefulness of a service layer in the structure of Laravel?
It’s just about separating the code from the Controller?
How to use this layer correctly?
My biggest question is whether it is worth increasing the amount of code in a medium-sized application to enter with the service layer. Often small pieces of code can be written to the controller itself to insert/update/delete a record. Only it bothers me to directly access the bank in the controller, so I use services.
But, I think if one part of the application uses one or more services to perform bank operations according to the business rule, all other parts of the application must also use, this includes those actions that have only one line. Am I thinking wrong? Should I use services only in more complex actions (for example, a user registration)?
I’m sensing my codes are getting more verbose because of this.
For example, to update a user name, I could simply do:
function update(User $user) {
$user->name = 'Foo';
$user->save();
}
But instead I do:
function update(User $user) {
$this->userService->updateUser($user); //Aqui aplico a regra de negócio, num outro método em outra classe, que talvez nem fosse necessária
}
What would be more correct?
It really doesn’t need mainly if it is to make methods that already exist. I think the layer of
Model
Eloquent already does what it needs and is enough. The only thing I use is to simplify within theModel
filter calls, orders, junctions etc ... that is, I can solve everything in the same class, instead of instantiating another ...– novic