Let Claude Implement the Repository Pattern in Your Codebase
Direct Eloquent calls scattered across controllers make code hard to test and even harder to swap out later. The Repository pattern fixes this — and Claude can retrofit it onto existing code without breaking anything.
Show Claude a controller that's doing too much and ask it to extract a repository:
Refactor this controller so all database access goes through a repository.
Create a UserRepositoryInterface and an EloquentUserRepository implementation.
Bind the interface in a service provider. Keep the controller thin.
@app/Http/Controllers/UserController.php
Claude will generate the interface, the Eloquent implementation, update the service provider, and inject the repository into the controller via the constructor — complete with updated tests that mock the interface rather than hitting the database.
For bigger refactors, ask Claude to do one model at a time and generate the full set of files:
Create a repository for the Order model. Include methods for:
findById, findByUser, findPendingOlderThan(Carbon $date), and save.
Write a mock in tests/Fakes/ that I can use in unit tests.
Repositories are most valuable as seams for testing — having Claude generate them means you get the interface, implementation, and test fake in one go.
Log in to leave a comment.
The /security-review command scans your uncommitted changes for injection vectors, auth gaps, hardcoded secrets, and other common vulnerabilities.
The SessionStart hook fires when any session begins or resumes, making it ideal for loading environment variables and running one-time setup scripts.
Ask Claude to write property-based tests for your functions using fast-check — it identifies the mathematical invariants in your code and generates tests that cover inputs you'd never enumerate by hand.