Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NoobDev0410's avatar

Laravel Services Or Actions or None

I have been watching videos and reading articles regarding action classes and service classes recently how ever still i don't know a exact use case to use them. i tend to use custom created helper methods inside the controller and the normal logic. should we consider to use actions or services if our code in the controller grows with new features like jobs , notifications and other related model updates?

Any answers would be appreciated

Thank you

0 likes
4 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Action classes are like helper functions. They should work in isolation, be passed the parameters they need, and (optionally) return a result. An action class holds no state.

Service classes are more for related functions, that can be called from anywhere in your application. For instance, I would probably implement a shopping cart as a service. The service might initiate a basket, add or remove items, return a total, persist the basket to storage etc. A service class can hold state.

You would not code cart functions in a controller because multiple controllers could require the services of a cart.

You could put the functionality of an action as a controller function, but as soon as you need that same function in other places, it becomes a candidate to be removed to an action.

Hope that helps clarify the difference.

2 likes
NoobDev0410's avatar

@Snapey Thanks sir for the explanation. if we have lots of code and operations in the controller could we refactor it may be we use an action in that case. combined with jobs or some other thing. in that case is it considered okay to dispatch jobs/ broad cast inside the handle method of an action and can we justify the SRP by considering its all responsible to "name of our functionality"?

Furthermore if we create an action is it fine to DI another action inside our action to reuse its method if our action needs to use it. ?

Any answer would be appreciated

Thank you

Snapey's avatar

@NoobDev0410 No problem using actions within actions (or jobs or email, etc)

For instance, an action might be to ProcessInvoice. This could include sending an email (via a mailable), posting a record to an accounts system (via Job), updating the database record.

Watch out for actions that depend on things like Http request objects that would restrict the use of the Action.

2 likes

Please or to participate in this conversation.