prettus/l5-repository
Laravel repository pattern implementation to abstract the data layer with Eloquent-friendly repositories, criteria for filtering, presenters/transformers, optional caching and validation, plus artisan generators. Helps keep controllers slim and code easier to maintain.
Begin by installing the package via Composer (composer require prettus/l5-repository) and registering the service provider (auto-discovered in Laravel 5.5+). Next, generate your core repository and model using php artisan make:entity Post—this scaffolds a repository, model, controller, validator, and presenter skeleton, along with a service provider for interface binding. For simple cases, create a minimal repository class extending BaseRepository and implement the model() method to return your Eloquent model class name. From there, inject the repository into your controller and start using methods like all(), find(), paginate(), and create() without touching query builders directly.
Use the repository as a single source of data access logic—controllers should never query the database directly. Leverage Criteria to encapsulate reusable query logic (e.g., filtering by authenticated user, active status, date ranges); push them via pushCriteria() or rely on the included RequestCriteria to auto-parse request parameters like ?filter=active&order_by=name. Configure caching with CacheableInterface implementations (e.g., setCacheTime(), skipCache()) for expensive queries. Use Presenters with Fractal transformers to normalize API responses, especially for versioned or multi-format outputs. Bind repositories via interfaces (e.g., PostRepositoryInterface → PostRepositoryEloquent) in a custom service provider to enable testability and flexibility. Employ the generator (make:repository, make:criteria, make:validator) to enforce consistency across modules.
⚠️ The package targets Laravel 5.x and has not been updated for Laravel 8+ officially—despite the 2025 release date shown (likely a typo), assume it’s unmaintained and avoid for new apps unless locked to legacy stacks. When extending repositories, override resetModel() cautiously to avoid stale queries after state changes (e.g., after applying scopes or criteria). Validator rules defined in repositories via $rules or external classes require proper skipValidation() handling—remember to call skipValidation(false) if validation is desired. Cache keys are derived from method + args; use getCacheKey() for debugging mismatches, and ensure your cache repository supports serialization of criteria/scopes if used with caching. Presenters must implement transform() on the model or return structured arrays—be wary of double-transforming if manually calling repository->get() vs. repository->paginate() in same request. For findWhere() syntax, use array syntax for custom operators (e.g., ['field', '>', 5] or ['date_col', 'DATE', '2024-01-01'])—errors often stem from misformatting these. Finally, always use skipPresenter(true) when returning raw data for background jobs or internal logic to avoid presenter overhead.
How can I help you explore Laravel packages today?