- How do I install laraditz/repository in a Laravel project?
- Run `composer require laraditz/repository` to install the package. No additional configuration is required—just create repository classes extending the provided base class and inject them into your services or controllers. The package works with Laravel 8+ and PHP 8.x.
- Does this package support Laravel 9 or 10?
- While officially tested on Laravel 8+, laraditz/repository should work with Laravel 9 and 10 due to its minimal dependencies. However, test thoroughly in your environment, especially if using newer Eloquent features like model macros or query builder enhancements.
- Can I use repositories for soft-deleted Eloquent models?
- Yes, but you must configure soft deletes at the model level (e.g., `use SoftDeletes;` trait). The repository will inherit this behavior automatically. No additional repository-specific setup is needed unless you want to customize how soft-deleted records are handled in queries.
- How do I create a custom repository method for complex queries?
- Extend the base repository class and define custom methods using Eloquent’s query builder. For example, add a `scopeActive()` method to filter records, then call it via `$repository->active()->get()`. This keeps your business logic centralized while allowing flexible queries.
- Will repositories improve my Laravel app’s testability?
- Absolutely. By abstracting Eloquent interactions behind repositories, you can mock dependencies in unit tests (e.g., `MockRepository::shouldReceive('find')->andReturn($fakeModel)`). This isolates business logic from database concerns, making tests faster and more reliable.
- Does this package work with Laravel’s first-party testing tools (e.g., Pest or PHPUnit)?
- Yes, it integrates seamlessly with Laravel’s testing tools. Use dependency injection to pass repositories to your test classes, then mock them as needed. The package doesn’t impose any testing-specific constraints, so you retain full control over your test suite.
- Are there performance concerns with using repositories in production?
- Repositories add minimal overhead for simple CRUD operations. However, avoid overusing them for trivial queries (e.g., fetching a single record). For complex queries, consider caching results at the repository level or using Laravel’s query caching features.
- How does this compare to spatie/laravel-query-builder or other repository packages?
- laraditz/repository is lightweight and focuses solely on wrapping Eloquent models, while spatie/laravel-query-builder offers advanced query-building tools. If you need complex queries or ORM-agnostic support, spatie’s package may be better. For clean CRUD abstraction, this is a simpler alternative.
- Can I use repositories with Laravel’s API resources or livewire?
- Yes, repositories work well with both. For API resources, inject the repository into your controller and pass the results to the resource. For Livewire, use repositories to fetch data in the component’s `mount()` or `hydrate()` methods, keeping your logic decoupled.
- What’s the best way to handle transactions with repositories?
- Use Laravel’s `DB::transaction()` or Eloquent’s `beginTransaction()` directly in your service layer. Repositories don’t enforce transaction boundaries—this keeps them framework-agnostic. For example, wrap multiple repository calls in a transaction to ensure data consistency.