3

I’m refactoring my Laravel application to follow a clean architecture approach with clear separation of concerns:

Controller → Service → Repository

The Controller handles the HTTP layer,the Service contains business logic, and the Repository is responsible for data persistence (Eloquent, API, or cache).

Problem

I’m trying to design my UserRepositoryInterface to be fully abstract and independent of Laravel’s Eloquent implementation, ensuring that higher layers (like services or controllers) depend only on a data-access contract rather than a specific ORM or framework.

Here’s the current design:

interface UserRepositoryInterface
{
    public function findClientById(int $id): ?User;
    public function update(User $user, array $data): User;
}

The implementation looks like this:

class EloquentUserRepository implements UserRepositoryInterface
{
    public function findClientById(int $id): ?User
    {
        return User::role('client')->find($id);
    }

    public function update(User $user, array $data): User
    {
        $user->fill($data);
        $user->save();
        return $user;
    }
}

However, this ties the interface to Eloquent (User $user). If I switch to an API-based repository, this will break, since there’s no Eloquent model anymore.

Possible alternative

I was thinking of changing it to use a Data Transfer Object (DTO):

interface UserRepositoryInterface
{
    public function findClientById(int $id): ?UserDTO;
    public function update(int $id, UserDTO $dto): mixed;
}

That way, both Eloquent-based and API-based repositories can share the same interface while staying decoupled from the ORM.

My question

From a senior-level laravel architecture / clean architecture perspective:

  • Is it better for repositories to work directly with Eloquent models (User $user) since they’re data-specific anyway?

  • Or should I pass DTOs (or domain entities) to keep the repository layer framework-agnostic?

  • If DTOs are better, should the conversion (from Eloquent → DTO or vice versa) happen in the repository or service layer?

2
  • 1
    DO NOT use repositories with ActiveRecord. JUST DO NOT Commented Oct 28 at 20:19
  • I agree with Maksim. If you want to mix the Repository pattern (for data access) with Laravel, look into replacing Eloquent with Doctrine as your main ORM. Commented Oct 29 at 17:45

1 Answer 1

1

If you are using Eloquent you are already using Laravel so you cant really be framework agnostic.

Laravel will play nice with any other framework you might choose to use, but I think you will probably just stay with Laravel.

Use User $user

Sign up to request clarification or add additional context in comments.

1 Comment

I agree. You don't choose a framework and then decide to swap out the main packages. If you register the repo in a provider and typehint the interface you can in theory swap out to another repository class easy enough. Given the OP example of api use, you could you a DTO to return from the controller/service classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.