- What is forecho/laravel-repository and what problem does it solve?
- It is a small Laravel package that provides a generic repository implementation plus a few extras, so database queries live in dedicated repository classes instead of being scattered across controllers, models, or Blade views. You extend the provided base repository, put your query logic there, and inject it where needed. The result is thinner controllers, leaner Eloquent models, and data access code that is easier to reuse and test.
- How do I install forecho/laravel-repository with Composer?
- Install it like any other Laravel package by running composer require forecho/laravel-repository in the root of your project. Composer will pull the package and register it through Laravel's package discovery. Once installed, you can start creating repository classes that extend the package's base repository. Check the README and composer.json for the exact version constraint that matches your Laravel release.
- Do I need to register a service provider or publish config files manually?
- For typical Laravel applications the package is discovered automatically, so no manual provider entry is required. If a config file or other assets need publishing, the README will list the vendor:publish command to run. Always confirm against the README for the version you installed, since publishing steps can change between releases.
- How do I create my own repository class using this package?
- Create a new class that extends the package's base repository and tell it which Eloquent model it should work with, usually through a model property or constructor. You then add your own methods for the queries your application needs, while inheriting the common CRUD-style helpers. Bind the repository to its interface or class in a service provider if you want to inject it into controllers and jobs.
- Which Laravel and PHP versions does forecho/laravel-repository support?
- Support depends on the release you install, so check the require section of the package's composer.json before adding it to an older application. Packages of this type usually track recent Laravel versions and require a matching PHP version. If you are on an unsupported Laravel release, Composer will report a conflict at install time rather than failing silently at runtime.
- Does the package only work with Eloquent, or can I use other data sources?
- The package is built around Laravel's Eloquent models, which is where the base repository and CRUD helpers are most useful. If you need a different backing store, you can still keep the same repository interface and write your own implementation without extending the base class. That keeps your controllers depending on an abstraction rather than on Eloquent directly.
- Can I use dependency injection and interfaces with these repositories in controllers?
- Yes. The usual approach is to type-hint the repository class or an interface in your controller constructor and bind the concrete implementation in a service provider. Laravel's container then resolves it automatically for each request. This also makes it trivial to swap in a fake or in-memory implementation during tests.
- How do I test code that uses a repository from this package?
- Because queries are isolated in repository classes, you can unit test them directly against an in-memory SQLite database using Laravel's RefreshDatabase trait. For controllers and services, bind a mock or stub of the repository interface in the container so no database is touched at all. This separation is one of the main practical benefits of adding a repository layer.
- Does the repository layer add performance overhead in production?
- No meaningful overhead: a repository is just an extra PHP class calling the same Eloquent query builder, so the SQL sent to your database is unchanged. The only cost is an extra object resolved from the container per request, which is negligible. Real performance concerns come from query design, such as missing indexes or N+1 queries, not from the repository itself.
- How does it compare with prettus/l5-repository or similar Laravel repository packages?
- forecho/laravel-repository is intentionally minimal, offering a base repository with common CRUD helpers rather than a large feature set with criteria classes, presenters, and caching. That makes it easier to read and adapt if you only want a thin layer between controllers and models. If you need built-in filters, Fractal presenters, or cache decorators out of the box, a heavier package such as prettus/l5-repository may suit you better. Before adopting either, check recent commit activity and Laravel version support to confirm ongoing maintenance.