By Farhan Kayani, Senior Backend Developer.
A practical guide to separating HTTP handling, business logic and persistence in Laravel, with an example and trade-offs.
Laravel already offers useful abstractions through Eloquent, validation and its service container. A service-repository structure can help when an application develops business workflows that are difficult to maintain inside HTTP controllers. It is a design choice, not a mandatory Laravel convention.
- Separate responsibilities Controller: handles HTTP input, authorization and responses. Service: coordinates business rules and use cases. Repository: encapsulates persistence operations when a meaningful boundary is needed.
- Define the persistence contract The example assumes a standard Laravel User model. A repository interface makes the dependency explicit.
// app/Contracts/UserRepository.php namespace App\Contracts; use App\Models\User; interface UserRepository { public function findOrFail(int $id): User; public function save(User $user): void; } // app/Repositories/EloquentUserRepository.php namespace App\Repositories; use App\Contracts\UserRepository; use App\Models\User; class EloquentUserRepository implements UserRepository { public function findOrFail(int $id): User { return User::query()->findOrFail($id); } public function save(User $user): void { $user->save(); } } - Put the use case in a service The service handles the workflow and delegates reads and writes to the repository.
// app/Services/UserService.php namespace App\Services; use App\Contracts\UserRepository; class UserService { public function __construct(private UserRepository $users) {} public function rename(int $id, string $name): void { $user = $this->users->findOrFail($id); $user->name = $name; $this->users->save($user); } } - Bind the interface and call the service Register the implementation in the service container. Laravel can then resolve the interface when constructing the service.
// Inside AppServiceProvider::register() $this->app->bind( \App\Contracts\UserRepository::class, \App\Repositories\EloquentUserRepository::class ); // Inside a controller method; a FormRequest validates input. public function update(UpdateUserRequest $request, int $id, UserService $users) { // Apply an appropriate authorization policy before updating. $users->rename($id, $request->validated('name')); return response()->noContent(); } This controller excerpt is illustrative, not a complete secure endpoint. Define the FormRequest validation, imports, route authentication and authorization policy in your application before using it.
- Consider testing and transactions Unit test the service with a fake repository when it helps isolate business rules. Use database-backed feature tests to check real Eloquent queries, constraints and authorization. If a workflow writes several related records, define an appropriate transaction boundary and consider concurrency and failure recovery. A simple single-record update does not automatically need a separate transaction wrapper.
- Know when not to use a repository For straightforward CRUD, an Eloquent model or a small service may be clearer. Wrapping every model method in a repository adds boilerplate without necessarily improving maintainability. Introduce an abstraction when it isolates meaningful query behavior, business dependencies or persistence details. Conclusion Keep controllers focused, make business rules explicit and choose boundaries that fit the application. Maintainability comes from clear responsibilities and good tests, not the number of architectural layers. Further reading: Laravel service container and Eloquent documentation. Originally published on my portfolio.
