D
D
Denis2021-01-17 19:36:38
Laravel
Denis, 2021-01-17 19:36:38

How to extract logic from models?

Now at me the main logic of work with a DB is taken out in services. They pull models and do something. But in fact, all methods in services are static. don't store anything at all. In fact, just a set of functions responsible for a particular model. Well, if you think about it, then there is no difference between the model itself, in general. Of course, you can store an instance of the model in the service, but there is no difference between Auth::user() and $this->getUser(); even worse. What would be the best way to do it? There is no business logic, in fact, only CRUD with models. Leave everything in the models and pull their methods directly from the controller? The project is simple, no abstractions, entities, please do not suggest :) it is full of useless repositories.

Is there a way to scatter the code in the model into different classes, grouping them according to a common meaning, but so that they are all one model? More specifically, behavior. Or use services like me is it ok?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jazzus, 2021-01-17
@Gromfer

Is there a way to scatter the code in the model into different classes, grouping them according to a common meaning, but so that they are all one model?

Traits.
but it doesn't matter whether Auth::user() or $this->getUser();

there is a difference. In the first case, I understand that I will get an authorized user. In the second I will get some kind of user incomprehensible. And if I open the method and see Auth::user() there will be a facepalm)
fed up with useless repositories

Well, this is OOP. So far, the project has no simple meaning. Then it gets a little more complicated and the meaning appears.
In general, the question is abstract and useless without code. Because something in models, something in services, and a lot of things in other Laravel classes that are often not used.

I
Ilya Chubarov, 2021-01-18
@agoalofalife

First of all, repositories are not useless.
Secondly, everything that has been invented or is being invented makes sense, but not everywhere and not always.
If you have a simple CRUD application why do you need services?
Most likely you wanted to offload the controllers and actually moved the code from one place to another.
And what kind of code can be shared if it's just CRUD.
Eloquent already has all the magic to create crud

User::create()
User::update()
User::find()

If you have something more, it means some kind of business - there is still logic.
I see two ways:
- This is to read articles and books on architecture, Fowler, Evans Eric, Vernon Vaughn will do .. Since you are drawn there, you can do it in parallel
- Just write further in the spirit of Laravel, but the first does not interfere with the second.
For starters, read this one.
If you read everything many times, there will be even more questions, go for it!

D
dmitriy, 2021-01-18
@dmitriylanets

I use CommandBus and Command to organize business logic, change the state of entities, in Services, if it remains, then only to receive data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question