Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
L5 Repository

L5 Repository Laravel Package

prettus/l5-repository

Prettus/l5-repository abstracts your Laravel app's data layer behind repositories, with built-in criteria, presenters, caching, validators, and generators. It keeps query logic reusable and controllers clean, making Eloquent-backed applications easier to maintain.

View on GitHub
Deep Wiki
Context7

prettus/l5-repository brings the Repository pattern to Laravel, abstracting the data layer so applications stay flexible and maintainable. It ships base repository classes and contracts that decouple business logic from Eloquent models, letting you swap implementations without touching controllers.

Repositories support criteria for composable queries, built-in caching, presenters for output transformation, and validators, plus Artisan generators to scaffold classes quickly.

  • Repository & Criteria interfaces for clean, composable query logic
  • Cacheable repositories with configurable lifetimes and keys
  • Presenters and validators for transformed output and safe input
  • Artisan generators for repositories, criteria, and presenters
  • RequestCriteria for dynamic filtering, sorting, and searching
Frequently asked questions about L5 Repository
How do I install and set up prettus/l5-repository in Laravel 10+?
Run `composer require prettus/l5-repository` and publish the config with `php artisan vendor:publish --provider="Prettus\Repository\Providers\RepositoryServiceProvider"`. Then register the service provider in `config/app.php` and run `php artisan make:bindings` to auto-bind repositories to interfaces.
Does this package work with Laravel 13’s new features like Eloquent 10?
Yes, the package is compatible with Laravel 13 and Eloquent 10. It leverages Eloquent’s latest features while maintaining backward compatibility with older Laravel versions (8-13). Always check the [migration guides](https://github.com/andersao/l5-repository/tree/2.0.14) for version-specific updates.
Can I use repositories for simple CRUD operations, or is it overkill?
While repositories shine for complex logic, they’re also useful for CRUD. However, for tiny projects, direct Eloquent calls might be simpler. The package’s generators (`make:entity`, `make:repository`) automate boilerplate, so the overhead is minimal. Evaluate based on your app’s complexity and team’s familiarity with the pattern.
How do I implement custom filtering (e.g., API query params) with Criteria?
Create a custom Criteria class by extending `Prettus\Repository\Criteria\Criteria` and implementing your logic in the `apply` method. Use `RequestCriteria` to dynamically filter based on HTTP request parameters. Example: `return $this->model->where('status', $this->request->input('status'));` in your Criteria class.
What’s the best way to handle caching with repositories?
Enable caching by implementing `CacheableInterface` in your repository and configuring cache drivers in `config/repository.php`. Use `cacheFor()` to specify cache keys and `getCacheTime()` to set TTL. For invalidation, manually clear cache when data changes or use events like `eloquent.updated` to trigger cache purging.
How do Presenters/Transformers work, and should I use Fractal?
Presenters transform model data into a structured format (e.g., API responses). The package includes basic Presenters, but integrating [Fractal](https://fractal.thephpleague.com/) is recommended for advanced use cases. Example: `return $this->presenter->present($model, new UserPresenter());` in your repository’s `find()` method.
Will this package slow down my application if I don’t use caching?
Without caching, performance depends on your query complexity. Criteria stacking can cause N+1 issues if not optimized. Use Laravel Debugbar to profile queries and consider eager loading (`with()`) or raw SQL in Criteria for performance-critical paths. Caching is optional but highly recommended for read-heavy apps.
How do I migrate from direct Eloquent calls to repositories in existing controllers?
Replace `Model::find()` or `Model::all()` with `$repository->find()` or `$repository->all()`. Inject repositories via constructor (dependency injection) and update method calls. Use `php artisan make:entity User` to generate a repository for an existing model, then replace controller logic incrementally.
Are there alternatives to prettus/l5-repository for Laravel?
Yes, alternatives include `spatie/laravel-query-builder` (for dynamic filtering), `laravel-eloquent` (for advanced query scopes), or `orchid/platform` (for admin panels with built-in repositories). However, `prettus/l5-repository` stands out for its comprehensive feature set (Criteria, Presenters, caching) and Laravel-specific optimizations.
How do I test repositories, and does it improve testability?
Repositories improve testability by abstracting database logic. Mock the repository interface in unit tests (e.g., `Mockery::mock(RepositoryInterface::class)`) to isolate business logic. For integration tests, use `RefreshDatabase` trait to test repository methods against a real database. Example: `public function testFindUser() { $this->assertEquals($user, $repository->find(1)); }`
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections