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

ctrlaltdelme's avatar

When to create a Service?

I'm trying to determine when it is necessary or best practice to create a Service in a Laravel application. I can't seem to find too much in Laravel's documentation. Hoping to get some help here! Thanks!

0 likes
4 replies
tisuchi's avatar

@ctrlaltdelme ​Generally in Laravel, create a service class when you need to encapsulate complex business logic or operations that are reused across multiple controllers, promoting cleaner and more maintainable code. ​

Maybe you can take a look follow articles to get some more ideas:

2 likes
martinbean's avatar

@ctrlaltdelme A “service” is just a class that holds business logic. You’d extract code to a service if it was becoming too unwieldy to have in a controller, or if you were re-using business logic in multiple places (i.e. you wanted to perform the same action in a controller but also a console command).

So, say you could delete an account, but deleting an account has lots of steps, and an account could be deleted from both an admin panel as well as an Artisan command. Instead of copying the logic from the controller into the Artisan command’s body, you could instead create an “account service” that holds the logic, and use that method in both the controller and Artisan command.

class AccountService
{
    public function delete(Account $account)
    {
        // All the logic to delete and clean up an account...
    }
}
class AccountController extends Controller
{
    protected AccountService $accountService;

    public function __construct(AccountService $accountService)
    {
        $this->accountService = $accountService;
    }

    public function delete(Account $account)
    {
        $this->accountService->delete($account);
    }
}
class DeleteAccountCommand extends Command
{
    protected $signature = 'accounts:delete {id}';
    protected $description = 'Delete and clean up an account.';

    public function handle(AccountService $accountService): int
    {
        $account = Account::query()->findOrFail($this->argument('id'));

        $accountService->delete($account);

        return Command::SUCCESS;
    }
}
2 likes
ctrlaltdelme's avatar

@martinbean Awesome! That helps explain things! So, then let me ask this follow-up -- since it seems a Service is meant to help with keeping code clean and methods related to a route or Controller kept together with other related code, when would you put Helper methods inside the Model such as getAccountOwner or something similar, using your example? Am I thinking about this right?

jlrdw's avatar

@ctrlaltdelme

since it seems a Service is meant to help with keeping code clean and methods related to a route or Controller kept together with other related code

A service can be many things such as reusable code like:

Code to use the Length aware paginator. So I can use this from any controller.

A service as mentioned can also be to cut down on clutter, like a complex search routine I have is in a service I call from a controller.

when would you put Helper methods inside the Model such as getAccountOwner

You are referring to something different there accessors and mutators. Which is covered in the documentation. I still call it getters and setters.

Please or to participate in this conversation.