{"id":22125713,"url":"https://github.com/czim/laravel-repository","last_synced_at":"2025-04-07T15:10:59.669Z","repository":{"id":41508595,"uuid":"41598200","full_name":"czim/laravel-repository","owner":"czim","description":"Repository pattern implementation for Laravel","archived":false,"fork":false,"pushed_at":"2024-07-30T13:24:17.000Z","size":210,"stargazers_count":51,"open_issues_count":3,"forks_count":19,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T13:16:38.519Z","etag":null,"topics":["eloquent","laravel","laravel-repositories","repository","repository-pattern"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czim.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-29T15:40:08.000Z","updated_at":"2024-07-30T13:24:21.000Z","dependencies_parsed_at":"2024-12-01T16:50:12.917Z","dependency_job_id":null,"html_url":"https://github.com/czim/laravel-repository","commit_stats":{"total_commits":111,"total_committers":16,"mean_commits":6.9375,"dds":"0.49549549549549554","last_synced_commit":"67e45f05a41aba79b723f6e16ac63e2d04ac599d"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-repository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-repository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-repository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-repository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czim","download_url":"https://codeload.github.com/czim/laravel-repository/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247675607,"owners_count":20977378,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","laravel-repositories","repository","repository-pattern"],"created_at":"2024-12-01T16:37:17.957Z","updated_at":"2025-04-07T15:10:59.643Z","avatar_url":"https://github.com/czim.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Repository\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Latest Stable Version](http://img.shields.io/packagist/v/czim/laravel-repository.svg)](https://packagist.org/packages/czim/laravel-repository)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/029ab930-9064-4acf-8602-87f8c010f387/mini.png)](https://insight.sensiolabs.com/projects/029ab930-9064-4acf-8602-87f8c010f387)\n\nRepository setup inspired by the Bosnadev/Repository package. This package is an extended, adjusted (but entirely independent) version of that, with its own interfaces.\n\nOne major difference to the Bosnadev repository is that this one is able to deal with repeated and varying calls to the same repository instance, without breaking down or undesirable repeated application of Criteria.\nYou can instantiate a repository once and do anything with it in any order, and both queries and model manipulation methods will keep working.\n\nAmong the added functionality is the ability to override or 'temporarily' set and remove Criteria, post-processing models after retrieval.\n\nI'm well aware that there is *much* to say against using Repositories like this (and the repository pattern in general), but I find they have their uses.\nI prefer using them to make for easier unit testing in large projects.\n\n\u003e Note: I recommand against using this package. I'm making some updates for my personal legacy projects,\n\u003e but I consider this approach to be a serious antipattern (at least with Eloquent).\n\n## Version Compatibility\n\n| Laravel    | Package  |\n|:-----------|:---------|\n| 5.1        | 1.0      |\n| 5.2        | 1.2      |\n| 5.3        | 1.2      |\n| 5.4 to 5.8 | 1.4      |\n| 6.0        | 2.0      |\n| 7.0, 8.0   | 2.1      |\n| 9.0        | 3.0, 4.0 |\n\n### Warning\n\nVersion 4.0 has many breaking changes.\nRefer to the [Changelog](CHANGELOG.md) for details.\n\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require czim/laravel-repository\n```\n\nIf you want to use the repository generator through the `make:repository` Artisan command, add the `RepositoryServiceProvider` to your `config/app.php`:\n\n``` php\nCzim\\Repository\\RepositoryServiceProvider::class,\n```\n\nPublish the repostory configuration file.\n\n``` bash\nphp artisan vendor:publish --tag=\"repository\"\n```\n\n\n## Basic Usage\n\nSimply extend the (abstract) repository class of your choice, either `Czim\\Repository\\BaseRepository`, `Czim\\Repository\\ExtendedRepository` or `Czim\\Repository\\ExtendedPostProcessingRepository`.\n\nThe only abstract method that must be provided is the `model` method (this is just like the way Bosnadev's repositories are used).\n\n\n### Base- and Extended Repositories\n\nDepending on what you require, three different abstract repository classes may be extended:\n\n* `BaseRepository`\n\n    Only has the retrieval and simple manipulation methods (`create()`, `update()` and `delete()`), and Criteria handling.\n\n* `ExtendedRepository`\n\n    Handles an **active** check for Models, which will by default exclude any model which will not have its `active` attribute set to true (configurable by setting `hasActive` and/or `activeColumn`).\n    Handles caching, using [dwightwatson/rememberable](https://github.com/dwightwatson/rememberable) by default (but you can use your own Caching Criteria if desired).\n    Allows you to set Model scopes, for when you want to use an Eloquent model scope to build your query.\n\n### Using the repository to retrieve models\n\nApart from the basic stuff (inspired by Bosnadev), there are some added methods for retrieval:\n\n* `query()`: returns an Eloquent\\Builder object reflecting the active criteria, for added flexibility\n* `count()`\n* `first()`\n* `findOrFail()`: just like `find()`, but throws an exception if nothing found\n* `firstOrFail()`: just like `first()`, but throws an exception if nothing found\n\nEvery retrieval method takes into account the currently active Criteria (including one-time overrides), see below.\n\nFor the `ExtendedPostProcessingRepository` goes that postprocessors affect all models returned, and so are applied in all the retrieval methods (`find()`, `firstOrFail()`, `all()`, `allCallback`, etc).\nThe `query()` method returns a Builder object and therefore circumvents postprocessing. If you want to manually use the postprocessors, simply call `postProcess()` on any Model or Collection of models.\n\n\n#### Handling Criteria\n\nJust like Bosnadev's repository, Criteria may be pushed onto the repository to build queries.\nIt is also possible to set default Criteria for the repository by overriding the `defaultCriteria()` method and returning a Collection of Criteria instances.\n\nCriteria may be defined or pushed onto the repository by **key**, like so:\n\n``` php\n    $repository-\u003epushCriteria(new SomeCriteria(), 'KeyForCriteria');\n```\n\nThis allows you to later remove the Criteria by referring to its key:\n\n``` php\n    // you can remove Criteria by key\n    $repository-\u003eremoveCriteria('KeyForCriteria');\n```\n\nTo change the Criteria that are to be used only for one call, there are helper methods that will preserve your currently active Criteria.\nIf you use any of the following, the active Criteria are applied (insofar they are not removed or overridden), and additional Criteria are applied only for the next retrieval method.\n\n``` php\n    // you can push one-time Criteria\n    $repository-\u003epushCriteriaOnce(new SomeOtherCriteria());\n\n    // you can override active criteria once by using its key\n    $repository-\u003epushCriteriaOnce(new SomeOtherCriteria(), 'KeyForCriteria');\n\n    // you can remove Criteria *only* for the next retrieval, by key\n    $repository-\u003eremoveCriteriaOnce('KeyForCriteria');\n```\n\nNote that this means that *only* Criteria that have keys can be removed or overridden this way.\nA `CriteriaKey` Enum is provided to more easily refer to the standard keys used in the `ExtendedRepository`, such as 'active', 'cache' and 'scope'.\n\n\n## Configuration\nNo configuration is required to start using the repository. You use it by extending an abstract repository class of your choice.\n\n### Extending the classes\nSome properties and methods may be extended for tweaking the way things work.\nFor now there is no documentation about this (I will add some later), but the repository classes contain many comments to help you find your way (mainly check the `ExtendedRepository` class).\n\n### Traits\nAdditionally, there are some traits that may be used to extend the functionality of the repositories, see `Czim\\Repository\\Traits`:\n\n* `FindsModelsByTranslationTrait` (only useful in combination with the [dimsav/laravel-translatable](https://github.com/dimsav/laravel-translatable) package)\n* `HandlesEloquentRelationManipulationTrait`\n* `HandlesEloquentSavingTrait`\n* `HandlesListifyModelsTrait` (only useful in combination with the [lookitsatravis/listify](https://github.com/lookitsatravis/listify) package)\n\nI've added these mainly because they may help in using the repository pattern as a means to make unit testing possible without having to mock Eloquent models.\n\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n\n## Credits\n\n- [Coen Zimmerman][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/czim/laravel-repository.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/czim/laravel-repository.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/czim/laravel-repository\n[link-downloads]: https://packagist.org/packages/czim/laravel-repository\n[link-author]: https://github.com/czim\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczim%2Flaravel-repository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczim%2Flaravel-repository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczim%2Flaravel-repository/lists"}