- How do I install and set up sajadsdi/laravel-repository in a Laravel 11 project?
- Run `composer require sajadsdi/laravel-repository`, then extend the base `Repository` class for each Eloquent model. Define abstract methods like `getModelName()`, `getSearchable()`, `getFilterable()`, and `getSortable()` in your custom repository class to configure behavior. No additional configuration is needed for basic usage.
- Does this package support Laravel 9.x or older versions?
- The package officially supports Laravel 10 and 11 as of 2024. While it may work with Laravel 9.x, backward compatibility isn’t guaranteed, and you may need to adjust for breaking changes in newer Laravel versions. Always test thoroughly before migrating.
- Can I use this package for complex queries like joins or subqueries?
- Yes, the package supports advanced joins and query building. Use the `with()` method for eager loading relations or define custom query scopes in your repository. For complex logic, leverage Eloquent’s query builder directly within repository methods.
- How does this package handle pagination and sorting?
- Pagination is built-in via Laravel’s native pagination methods (e.g., `paginate()`). Sorting is configurable via `getSortable()` and applied dynamically using the `sortBy()` method. You can also chain multiple sorting rules for multi-column sorting.
- Will repositories slow down my application? How can I optimize performance?
- Repositories add minimal overhead for simple CRUD operations. For performance-critical queries, use caching via `remember()` or `forget()` methods. Avoid over-fetching data by leveraging Eloquent’s lazy loading and repository-level query optimization.
- Can I integrate this package with Laravel’s events or observers?
- Yes, the package supports event-driven hooks (e.g., `Creating`, `Updating`) for pre/post-operation logic. Replace model observers with repository events to centralize business logic. Events are triggered automatically when using repository CRUD methods.
- How do I test repositories in PHPUnit? Can I mock database calls?
- Repositories are designed for testability. Use Laravel’s mocking tools to replace the underlying Eloquent model with a fake implementation. For example, mock `User::query()` in your tests to isolate repository logic from the database.
- What’s the best way to migrate from direct Eloquent calls to repositories?
- Start by creating repositories for high-churn models (e.g., `User`, `Order`). Replace direct model calls in controllers/services incrementally. Use feature flags or deprecated methods to phase out old code. Audit your codebase first to identify dependencies on magic methods or global scopes.
- Does this package work with Laravel Scout or non-Eloquent databases?
- This package is optimized for Eloquent models and won’t work with non-Eloquent databases like MongoDB via Scout. For Scout, consider using a dedicated search package or implementing custom repository logic for search queries.
- Are there alternatives to this package for implementing the repository pattern in Laravel?
- Alternatives include `gloudemans/shoppinglist` (simpler), `spatie/laravel-query-builder` (query-focused), or rolling your own with interfaces and base classes. This package stands out for its built-in CRUD tools, dynamic method forwarding, and advanced filtering/search capabilities.