Skip to main content

Laravel Service and Repository Pattern Implementation

Implement clean service and repository layers in Laravel — separating business logic from controllers and database queries.

Vul de plaatshouders in

Edit the values, then copy your finished prompt.

Jouw Prompt
prompt.txt

                                

What this prompt does

This prompt asks the AI to implement the service and repository pattern for one business domain in a Laravel app, producing the full set of layers rather than a single class. It generates a repository interface and Eloquent implementation, a service holding the business logic, DTOs for moving data between layers, a service provider binding for dependency injection, a refactored controller that delegates to the service, Form Request validation, and both unit and integration tests.

The variables define the shape of the architecture. [domain] names the business area, [model_name] is the primary Eloquent model the repository wraps, and [business_operations] lists the operations the service must expose — these become its public methods. [architecture_style] sets the guiding principles (clean architecture with SOLID), which influences how strictly the layers are separated. Because the prompt treats DTOs and Form Requests as the contract between layers and asks for a mocked-repository unit test plus a real integration test, the output is testable by design rather than tangled controller logic.

When to use it

  • Pulling business logic out of fat controllers into a dedicated service layer
  • Establishing a repository abstraction so the rest of the app doesn't query Eloquent directly
  • Setting up a new domain in a growing Laravel app that needs clear boundaries
  • Making business logic unit-testable by mocking the data layer
  • Introducing DTOs as an explicit contract between controllers, services, and repositories
  • Standardizing how a team structures non-trivial features going forward

Example output

You get a stack of PHP files: an interface plus its Eloquent repository, a service class whose methods map to your [business_operations], DTO classes, a service provider registering the interface-to-implementation binding, a slimmed-down controller, Form Requests, and two test files — a unit test with a mocked repository and an integration test hitting the database. Custom exceptions handle error cases. The controller becomes thin, delegating to the service, which orchestrates the repository and returns DTOs.

Pro tips

  • Name [business_operations] precisely (create order, process payment, generate invoice) — each becomes a service method, so vague lists give vague services
  • Keep one repository per [model_name]; resist letting a service reach across many models, which erodes the boundary you're building
  • Use the DTOs as the real contract — don't let raw Eloquent models leak through the service into controllers
  • Register the interface binding in the generated service provider and confirm it resolves before writing more code against it
  • Treat the mocked-repository unit test as the spec for your service's behavior, and the integration test as proof the Eloquent layer actually works
  • Don't over-apply this to trivial CRUD; the pattern earns its keep on domains with real business rules, not simple lookups

Frequently Asked Questions

What is the difference between the service and the repository here?
The repository wraps data access for one model, hiding Eloquent queries behind an interface. The service holds business logic and orchestrates one or more repositories, exposing operations like create order or process payment to the controller.
Why does it generate both unit and integration tests?
The unit test mocks the repository to verify the service's business logic in isolation, while the integration test exercises the real Eloquent repository against the database. Together they prove both the logic and the data layer work as intended.
Is this pattern overkill for simple CRUD?
Often yes. The service and repository layers add indirection that pays off when a domain has real business rules, but for a plain lookup or basic CRUD it can be unnecessary ceremony you should skip.
Where do the DTOs fit in?
DTOs carry data between the controller, service, and repository so raw Eloquent models do not leak across boundaries. Treating them as the contract keeps each layer independent and makes refactoring one layer far less risky.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Meer in Laravel & PHP Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support