- 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)); }`