sostheblack/laravel-repository-pattern
A small Laravel package that adds a repository pattern implementation, providing an abstraction layer between your application logic and data sources so database queries can live in reusable repository classes for cleaner, more testable code.
composer.json. On Laravel 11 / PHP 8.2+, require ^0.2; on older Laravel 10 apps you can stay on 0.1.5 until you upgrade.
composer require sostheblack/laravel-repository-pattern:^0.2
php artisan about and check the package is listed. In Laravel 11 providers are registered via bootstrap/providers.php, but package discovery is unchanged — you should not need to add anything manually unless you disabled discovery.php artisan make:repository) and the default namespace/stub paths. These determine where generated interfaces and implementations land (app/Repositories, app/Contracts, etc.).Invoice, bind its interface in a service provider (or rely on the package's binding convention), inject the interface into a controller, and move a couple of scoped queries (forCustomer, unpaid, withRelations) out of the controller. This gives you an immediate, low-risk win and validates the binding wiring before you refactor broadly.public function __construct(private InvoiceRepositoryInterface $invoices) {}
with('lines', 'customer'), date filters, status filters). Keep business rules in services/actions; the repository should return models/collections, not make domain decisions.RepositoryServiceProvider rather than scattering them in AppServiceProvider. In Laravel 11 add that provider to bootstrap/providers.php.$this->app->bind(InvoiceRepositoryInterface::class, FakeInvoiceRepository::class);
For integration tests, resolve the real repository from the container so you exercise actual query behaviour.paginate, simplePaginate) behind repository methods so the HTTP layer stays consistent. If you add caching, do it in a decorator around the interface rather than inside the Eloquent implementation — that preserves the swap-ability the pattern is meant to provide.composer.json update. There are no new features or documented breaking changes, so an upgrade from 0.1.5 should be low risk — but re-run your test suite and check that the package's service provider still registers cleanly under Laravel 11's streamlined bootstrap.illuminate/* constraints. Run composer why-not / composer update --dry-run before merging into an app with pinned dependencies.Builder instances or accepting raw $columns/$where arrays from callers defeats the purpose. Keep return types to models, collections, paginators, or scalars.find() calls adds indirection without value. Reserve repositories for models with non-trivial query logic or where a data-source swap/mocking boundary is genuinely useful.Target [XInterface] is not instantiable error almost always means the interface isn't bound or the provider isn't registered (especially after moving to bootstrap/providers.php in Laravel 11). Verify with php artisan tinker → app(XInterface::class).with() defaults in the repository and expose explicit methods for variant loading.^0.2 and review changelogs on each bump; the 0.x versioning signals that minor releases can still carry compatibility-affecting changes such as this Laravel 11 constraint update.How can I help you explore Laravel packages today?