Clean Architecture and Domain-Driven Design in a Laravel application are not measured by the number of folders or by the presence of a Domain namespace. They earn their cost when business rules remain readable and testable, while delivery mechanisms and external providers can change without rewriting those rules. The objective is not to remove Laravel from the application. It is to use the framework effectively at clear boundaries and keep the core decisions visible. For a simple application, the cleanest architecture may have very few layers.
Begin with change pressure
Before moving code, identify the repeated pain. Are pricing rules duplicated across controllers? Is issuing an invoice impossible to test without an HTTP request? Are provider SDK types spread across the application? Does every small change require touching unrelated modules? Architecture should answer real change pressure. Applying a theoretical structure to a stable, simple product can increase cost without improving outcomes.
List decisions that change for different reasons. Business policy changes when the domain expert changes a rule. User interfaces change with interaction needs. Provider integrations change with an external contract. Persistence changes for operational reasons. Good boundaries keep one reason from dragging all the others into the same edit.
Build a shared domain language
DDD begins with language. If the business distinguishes an appointment, visit, claim, and approval, avoid collapsing them into Record and Status. Precise names expose behaviour and disagreement. “Cancel” may mean releasing a booking in one context and creating a reversing financial document in another. A generic cancellation method hides two different policies.
Maintain a small glossary for contested terms and use it in conversations, code, tests, and acceptance scenarios. Rename concepts when understanding improves. Ubiquitous language does not require turning every noun into a class; it requires important code to tell the same story as the domain expert.
Discover boundaries before creating modules
A bounded context is a boundary around meaning, not a technical folder. Scheduling and billing may have distinct models because “customer,” “status,” and “completion” mean different things. Map the information and events that cross the boundary. Avoid a universal model that accumulates every attribute simply because tables are related.
Start with boundaries inside one deployable application when that meets the need. A modular monolith can separate language and dependencies without introducing distributed transactions, network failure, and multiple release pipelines. Microservices are an operating model, not a remedy for unclear code.
Make the use case the unit of intent
A use case expresses an action such as RescheduleAppointment or IssueInvoice. It coordinates domain validation, transaction boundaries, and resulting events. A controller validates transport shape, invokes the use case, and maps the result to a response. A console command or queued handler can invoke the same application behaviour without duplicating policy.
Avoid vague service containers named Manager or Helpers. Give the unit a meaningful input, result, and dependency contract. Distinguish transport validation, such as a required field, from a domain rule, such as an approved invoice being ineligible for silent deletion. The first belongs near the edge; the second belongs with the model or use case.
Use value objects when they protect meaning
Money is not merely a decimal; it includes currency and rounding rules. An appointment period has a valid start and end. A tenant identifier should not be interchangeable with a user identifier. A value object is useful when it prevents an invalid state, centralises repeated behaviour, or makes an important contract explicit.
Do not wrap every string for aesthetic purity. Introduce the type when it removes a class of mistakes or makes rules easier to read. Prefer immutability, and test equality and serialization where the value crosses persistence or API boundaries.
Draw transaction boundaries around invariants
An aggregate is not an instruction to load every related record. It is a consistency boundary for invariants that must change together. If reserving a slot and preventing a conflict is one decision, design the appropriate atomic operation. Large reports can use focused read models without loading an entire behavioural graph.
External side effects cannot be protected by pretending a database transaction controls a remote API. Record the internal state and an outbox event or equivalent durable intent, then deliver it idempotently. Model retries, duplicate events, and compensation. A distributed transaction should not be assumed where the participants do not provide one.
Make integration interfaces belong to the application
An interface should express what the application needs, such as collecting a payment or submitting an invoice, rather than mirroring every method in a provider SDK. An adapter implements that contract and translates provider states and failures. External types stay at the edge, and tests can substitute a small fake that speaks the application’s language.
Do not create an interface for every class. A boundary deserves abstraction when it separates an external system, a meaningful alternative, or a useful test seam. Abstractions that protect no decision increase navigation cost. The Laravel multi-tenant architecture guide shows how explicit boundaries carry tenant context through persistence, jobs, cache, and files.
Use Eloquent deliberately
Eloquent can live in infrastructure, or serve directly inside a simple module when rules remain clear. Active Record itself is not the failure. The problem appears when a model becomes a container for validation, orchestration, provider calls, rendering concerns, and unrelated policies. Keep relationships, casts, and query scopes focused, and move multi-step use cases to named, testable units.
A repository should not recreate every Eloquent operation. Introduce a narrow repository contract when the domain needs specific collection behaviour, persistence must be isolated, or queries benefit from an application-owned interface. A generic find, save, and delete wrapper around every model usually adds ceremony rather than a boundary.
Model failure as part of the contract
Distinguish expected domain outcomes, retryable provider failures, and programming defects. “The slot is no longer available” is an outcome the interface can explain and recover from. A provider timeout may become a pending state and retry. An unexpected null or invariant violation deserves monitoring and investigation. A single catch-all exception removes those choices.
Translate provider and database errors at the boundary. Do not expose SQL or SDK messages to users. Preserve technical context in controlled logs without recording secrets or unnecessary personal data. Tests should assert the business outcome, not an internal exception sentence that may change.
Test the parts you own
Test value objects, domain policies, and use cases quickly without HTTP where practical. Use Laravel feature tests for routing, authentication, authorization, persistence configuration, and full workflows. Add contract tests for important adapters against supported simulations or provider test environments. You do not need to retest the framework, but you do need to test how your configuration and code use it.
Build fixtures in domain language rather than anonymous arrays. A test should explain why an action is accepted or rejected. When a defect is discovered, add the smallest test at the layer that can prevent its return, then add an end-to-end case if the integration path also matters.
Keep read models separate where useful
Operational screens and reports often need shapes that do not match write aggregates. A dedicated query object or projection can fetch the required columns efficiently without weakening domain invariants. This is a practical form of command-query separation, not a requirement to introduce a message bus.
Avoid returning Eloquent models directly across every boundary. Application result objects or stable arrays can protect callers from persistence changes where that protection has value. In a small CRUD flow, direct models may still be clearer; choose based on change and risk.
Measure and remove accidental complexity
Observe how long it takes a developer to locate the rule and how many files must change for a simple behaviour. If adding one field crosses six layers that protect no policy, the design is over-engineered. If every rule lives in one controller, it is under-structured. Use short architecture decision records for consequential choices, including the reason and conditions for review.
A basic administrative CRUD screen does not require an aggregate, repository, domain event, command bus, and factory when no domain rule exists. Start directly and extract a boundary when language, change pressure, or risk appears. Deliberate simplicity is consistent with DDD.
Refactor in vertical slices
Avoid stopping product work for a full rewrite. Select one high-change or high-risk journey, protect current behaviour with tests, extract a use case, and isolate one provider or policy. Keep the old and new paths observable during transition. A strangler approach can work inside a monolith as well as around a legacy service.
Measure whether the slice improves review clarity, defect isolation, test speed, and change effort. If it only produces more files, reconsider the pattern. The BarmajTek software delivery approach connects workflow discovery to staged technical decisions rather than beginning with a template.
Apply security at the boundaries and core
Authorization is not merely middleware. Resource and action policies belong close enough to the use case that another delivery mechanism cannot bypass them. Input validation protects the boundary, while domain invariants protect every caller. Secrets and provider credentials remain in infrastructure; the domain receives capabilities, not keys.
Threat modelling can identify which boundaries deserve stronger controls, audit, and tests. The architecture should make sensitive flows easier to find and review, not distribute them across implicit model hooks.
A worked example from ClinicTek: the patient import
Here is how one bounded context looks in a Laravel product we build and run. In ClinicTek, clinic staff upload a CSV or Excel file of patients. Before anything is saved, a preview counts the patients to import and lists the rows the importer already knows it will skip and why. Then staff import the valid rows. Our guide to migrating clinic data from Excel shows how a clinic prepares that file.
The import machinery lives in a shared package, barmajtek/saas-imports, which GymTek and TeachTek also use. It knows nothing about patients. ClinicTek adds the patient rules in small classes that implement four of the package's row contracts, and one definition class wires them together. The layout and code excerpts below are shortened from the code as of 4 November 2026, and we wrote their comments for this post:
packages/saas-imports/src/ shared: batches, rows, chunks, states; no product knowledge
Contracts/ Decoder, RowMapper, RowValidator, RowCommitter, RowRollback, ImportAuthorizer
Domain/ ImportState, InvalidRowPolicy, CommitMode, DeduplicationPolicy
Services/ PreviewService, ChunkCommitService, RollbackService
packages/saas-imports-spreadsheet/src/ adapter: XLS and XLSX decoding, kept out of the core
clinic_saas/app/Imports/
PatientImportDefinition.php wires one import type from its parts
Patients/ PatientHeaderMap, PatientMapper, PatientValidator,
PatientCommitter, PatientRollback, PatientDateOrder
The contracts describe a row's life in the application's own terms:
interface RowMapper
{
public function map(DecodedRow $row, ImportContext $context): MappedRow;
}
interface RowValidator
{
/** @return list<RowError> */
public function validate(MappedRow $row, ImportContext $context): array;
}
interface RowCommitter
{
// Make the idempotency key durable with the product write.
public function commit(MappedRow $row, CommitContext $context): RowResult;
}
interface RowRollback
{
// Idempotent: undo from RowResult::compensation, never by guessing.
public function rollback(MappedRow $row, RowResult $result, RollbackContext $context): void;
}
Three design decisions shape the import:
- The spreadsheet library stays at the edge. The core decodes CSV only. XLS and XLSX come from an adapter package that inspects the file before PhpSpreadsheet opens it. Upgrading or replacing the library touches that one package and none of the patient rules.
- Idempotency is part of the commit contract. An idempotent step gives the same result however many times it runs. Before ClinicTek's committer creates a patient, it records a hash of the row (its fingerprint) in an import ledger. When a queued chunk is retried after a failure, the committer finds that entry, returns the earlier result and creates no second patient.
- Rollback undoes only what the commit recorded. The committer returns compensation data: the record ID and a hash of what it wrote.
PatientRollbackrefuses to delete a patient whose record has changed since the import. It also refuses when the patient now has appointments, visits or invoices, because deleting that patient would break clinical or billing history. In those cases the rollback stops, the patient stays, and ClinicTek marks the import as not safely rolled back. A person then decides what to do with that record.
One domain rule came out of a review on 9 October 2026. The importer read a slash date such as 03/05/1990 month first, as PHP does. Clinics in Jordan write the day first, and English (US) Excel saves the month first. A small named class, PatientDateOrder, now makes that decision once per file. It looks only at dates that can be read one way:
public static function detect(iterable $values): string
{
$slash = $dayFirst = $monthFirst = false;
foreach ($values as $value) {
if (preg_match(self::SLASH_DATE, PatientMapper::normalizeDigits($value), $m) !== 1) {
continue;
}
$slash = true;
$dayFirst = $dayFirst || (int) $m[1] > 12;
$monthFirst = $monthFirst || (int) $m[2] > 12;
}
return match (true) {
$dayFirst && $monthFirst => self::MIXED, // both orders: rows with a date that reads two ways are skipped
$monthFirst => self::MONTH_FIRST, // saved by English (US) Excel
$slash => self::DAY_FIRST, // the Jordanian default
default => self::NONE,
};
}
When a file mixes both orders, the importer skips every row whose date could be read two ways. The preview names each such row and quotes its date. No wrong birth date reaches a patient record unseen.
The test that pins the behaviour works through the HTTP layer and asserts what the clinic sees:
it('deduplicates repeated patient imports without duplicate links or reporting', function (): void {
$csv = "name,phone,gender\nRepeated Patient,0795555555,female\n";
$mapping = ['name' => 0, 'phone' => 1, 'gender' => 2];
$first = uploadCsv($csv);
$this->actingAs($this->staff)
->postJson(importHost('/patients/import/'.$first['import_id'].'/run'), compact('mapping'))
->assertOk();
$second = uploadCsv($csv);
$this->actingAs($this->staff)
->postJson(importHost('/patients/import/'.$second['import_id'].'/run'), compact('mapping'))
->assertJsonPath('data.imported_count', 0)
->assertJsonPath('data.skipped_count', 1);
expect(Patient::query()->withoutGlobalScopes()
->where('tenant_id', $this->tenant->id)->count())->toBe(1);
});
The same file uploaded twice leaves one patient in the clinic. The ledger and fingerprint can change shape later without breaking this test. Pinning current behaviour with a test like this comes before any code moves when you migrate a running system without stopping operations.
Protect the decision that changes
Clean Architecture is not a fixed diagram. It is a dependency arrangement that keeps business policy readable, gives Laravel and providers clear roles, and places tests where failure matters. If a rule or external API change currently spreads across the system, request an architecture review of one high-change workflow. Judge the result by clarity and safe change, not by the number of patterns introduced.




