Service vs Action Classes: Key Differences
45sClearly explains the core difference between service and action classes, a common confusion among Laravel developers.
▶ Play Clip"The title accurately reflects the content, providing a clear and concise explanation of the difference."
The video explains the difference between service and action classes in Laravel. It provides examples of how each is used in controllers and queued jobs, and emphasizes that both are flexible PHP classes.
A service class groups methods around a specific Eloquent model or topic, with all public methods callable individually.
Services can be used in queued jobs by creating a new object and calling methods like getBrowser(), getOS(), getDevice().
In controllers, services are instantiated to call a single method, like getting the operating system.
Action classes typically have one method like handle() or invoke(), but can have more methods.
Action classes are used in controllers by creating a new object and calling the handle() method with parameters.
Both service and action classes are just PHP classes, so you can structure them however you want.
What is a service class in Laravel?
A service class groups methods around a specific Eloquent model or topic, with all public methods callable individually.
What is an action class in Laravel?
An action class typically has one method like handle() or invoke() to perform a single use case.
00:30
What is the key difference between service and action classes?
Services are broader, grouping related operations, while actions focus on a single task.
00:46
Service Class Definition
Clarifies that services group methods around a model or topic, not necessarily tied to a single model.
Action Class Definition
Highlights that actions typically have a single method, but can have more, offering flexibility.
00:30Flexibility in Structure
Emphasizes that both are just PHP classes, giving developers freedom to structure them as needed.
00:46[00:00] Service and action classes in Laravel. Typically service is a group of methods around the same eloquent model, but not necessarily, it could be one topic like user agent. And as you can see, all methods here are public, which means they can be called individually, or at the end there is a
[00:15] method to call multiple methods with one public method. And how that service is used, for example, in a queued job we create a new class object and then get browser, get OS, get device. Another example in controller, we also create a new object of that service, but in this case just to get the
[00:30] operating system. And here's an example of action class from another project. Typically action class has one method like handle or invoke, but no one is restricting you from creating more methods inside of the same action class like this. And this is how it is used in the controller, we create
[00:46] a new object of action, and then we just call one method handle with parameters. So I hope you see the difference between service and actions, but at the end of the day they are both PHP classes, and you can structure them however you want.
⚡ Saved you 0h 00m reading this? Transcribe any YouTube video for free — no signup needed.