Laravel Controller Code: Move to Model, Service, Action or Job?

preview_player
Показать описание
This is probably the no.1 debate in the Laravel community - "where should I put this code". Let me explain my philosophy, based on a practical example.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I do always create a directory called "Services" on each module of the application where lands the logic used by the controllers, so in my controllers I just pass The service class name as an argument to the controller method, and then I just call the service methof from the controller, my controller now all contains max 2 lines of code. My models contain only relationships, some overriding functions, and accessors/mutators. I can say this is a good practice

gtsmeg
Автор

It’s uncanny how often I’m thinking about a subject in laravel (that I could work more on) and you release a video on that very subject! Thanks as ever Povilas. 👏

gdogmalone
Автор

In my opinion, "move to model" is never an option, even for small projects. It's not models' responsibility to handle business logic and they should stay anemic. Services usually are the answer, but sometimes a service class handles too many responsibilities and methods. Better to use an action, which will handle a single aspect of the application, optionally injecting other services (each of one handling a specific responsibility).

eleftrik
Автор

Well, that's what I mean with a clear understanding of where what belongs. Thank you for this video, makes a lot more sense now. The Service Class looks the most readable for me. You explain things so well, that I understand things that I do even not understand after reading multiple times. Another big thank you for that! Crystal Clear

beatnu
Автор

Clicked on the LIKE button the moment he started talking about the topic because this was something that always troubled me. Looking forward to more of such videos.

adarshchacko
Автор

Perfect video! In this case I would have used a service. TO ME, actions does not return anything. You perfom an action and go back. Services could return something.
In this case an invoice is returned so I would have used a servece. IMHO, of course. :D

IlPandax
Автор

I also do like to use Services, i don't know why ppl go with rep pattern for small projects or push methods in model... As you said and i completely agree, model should consist of relations, fields, casts and scopes, all model related stuff...

brmley
Автор

all programmers confusing this part after learning the basics. they are going to code in the way showing in docs.
For small projects its OK. but for large or medium scale projects will be get more complex, hard to maintain

In My point of view

Models are not business logic, it represents database table. It is a gateway between application and database

WebController, Console Commands, Graphql Mutator, Graphql Query Resolvers, API controllers are receive input and output processed data, Not doing any operations itself. it controls everything between visible input and visible output

Actions Responsible for one specific operations assigned. action may call another action to execute.
Same action can use inside WebController, Console Commands, Graphql Mutator, Graphql Query Resolvers ... etc
I prefer Actions over Services to execute operations
It can be Queable

Services Performing computations over arguments and return its result
..etc

Key point is Split Responsibilities if it going to complex. It will help for maintenance, code reusing and unit testing

jomonjohnson
Автор

Totally agree with your preference of using Service classes! Love the high level comparison of ‘Model’ and ‘Service’!

adammenczykowski
Автор

Right on time. Few days ago I was structuring project functionality which can take some time: parse json, download files (~50mb), store them, update db. And I decided Jobs + Queue + Cron. After your video I'm staying with same decision. Thanks for content and QAP :)

dkvadratu
Автор

Thank you very much for such a clear explanation as always. Thanks to your videos I successfully passed my job interview for Laravel developer position, and explained things related to Laravel even made interviewer suprise :D

gaofan
Автор

It`s very interesting. I like Service too. I have been working with Angular for years and it has the same phylosophy respect with the actions. Usually I use just the methods controller for the logic but if the application is complex, have a long functions and code, it`s good idea sepatarate the logic in other class like Service. I think the same about models is for Eloquents operations...just that. Services it`s good idea for operations like actions. Jobs it`s for especific cases, for when you have to use a queue in a background.

jorgeluisbou-saad
Автор

I am quite new to Laravel and this video gives me good knowledge about Laravel. Thank you very much

denjand
Автор

Thanks for video
I have a project with complex business logic and long-run jobs.
I'm dispatching Events from Controller.
EventServiceProvider class stores the mapping between Listeners and Event (shouldDiscoverEvents = false) where one Event can have multiple Listeners. Job and Listener can be queueable (async) and run in the background.
Inside the controller, I'm validating the Request data, creating/updating the model, and then dispatching the Event.


Contoller -> Event -> Listener(s) -> Service(s)
Contoller -> Event -> Listener(s) -> Service(s) -> Job(s)
Contoller -> Event -> Listener(s) -> Job(s) -> Service(s)

SergiuSavva
Автор

Thanks a lot Povilas, I was trying to understand a way to separate the logic from my model Class to a Service class and your vídeo really helped me to get it. Thks for sharing

Samuel.Tedesque
Автор

Thank you for the info about the Repository pattern.

hamidimomov
Автор

For my projects I try to keep it simple and store the "service" methods on the model file. I find it easier this way. If it is a third party project, I follow whatever convention they have stablished.

viniciusalvess
Автор

I am writing all the logic in the controller, now i will follow your code structure. Its good to breakdown code.

mqxmrqp
Автор

Your videos are great, I can't even miss one second of them. Thanks for such a high quality content.

raminsadeghnasab
Автор

This is over my head. I wish I understood more what you are talking about, but it looks way advanced.

JustMillIt