{"id":28643494,"url":"https://github.com/giordanolima/eloquent-repository","last_synced_at":"2025-06-22T17:04:20.525Z","repository":{"id":62510889,"uuid":"82729156","full_name":"giordanolima/eloquent-repository","owner":"giordanolima","description":"Repository pattern for Eloquent ORM with focus in cache.","archived":false,"fork":false,"pushed_at":"2023-07-01T16:30:12.000Z","size":64,"stargazers_count":30,"open_issues_count":3,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-11T13:50:18.803Z","etag":null,"topics":["eloquent","laravel","laravel-package","repository","repository-pattern"],"latest_commit_sha":null,"homepage":null,"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/giordanolima.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-21T21:30:30.000Z","updated_at":"2022-12-14T22:40:57.000Z","dependencies_parsed_at":"2023-01-22T11:45:14.891Z","dependency_job_id":null,"html_url":"https://github.com/giordanolima/eloquent-repository","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/giordanolima/eloquent-repository","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giordanolima%2Feloquent-repository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giordanolima%2Feloquent-repository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giordanolima%2Feloquent-repository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giordanolima%2Feloquent-repository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giordanolima","download_url":"https://codeload.github.com/giordanolima/eloquent-repository/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giordanolima%2Feloquent-repository/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259546454,"owners_count":22874566,"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-package","repository","repository-pattern"],"created_at":"2025-06-12T23:06:02.779Z","updated_at":"2025-06-12T23:06:04.338Z","avatar_url":"https://github.com/giordanolima.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Repository\n[![Latest Stable Version](https://poser.pugx.org/giordanolima/eloquent-repository/v/stable)](https://packagist.org/packages/giordanolima/eloquent-repository) \n[![Total Downloads](https://poser.pugx.org/giordanolima/eloquent-repository/downloads)](https://packagist.org/packages/giordanolima/eloquent-repository) \n[![License](https://poser.pugx.org/giordanolima/eloquent-repository/license)](https://packagist.org/packages/giordanolima/eloquent-repository)\n[![StyleCI](https://styleci.io/repos/82729156/shield?branch=master)](https://styleci.io/repos/82729156)\n\n[Documentação em português.](README_PT.md)\n\nPackage to assist the implementation of the Repository Pattern using Eloquent ORM.\nThe main feature is the flexibility and ease of use and a powerful driver for query caching.\n## Installation\nInstalling via Composer\n```bash\ncomposer require giordanolima/eloquent-repository\n```\nTo configure the package options, declare the Service Provider in the `config / app.php` file.\n```php\n'providers' =\u003e [\n    ...\n    GiordanoLima\\EloquentRepository\\RepositoryServiceProvider::class,\n],\n```\n\u003e If you are using version 5.5 or higher of Laravel, the Service Provider is automatically recognized by Package Discover.\n\nTo publish the configuration file:\n```shell\nphp artisan vendor:publish\n```\n### Usage\nTo get started you need to create your repository class and extend the `BaseRepository` available in the package. You also have to set the *Model* that will be used to perform the queries.\nExample:\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nclass UserRepository extends BaseRepository\n{\n    protected function model() {\n        return \\App\\User::class;\n    }\n}\n```\nNow it is possible to perform queries in the same way as it is used in Elquent.\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nclass UserRepository extends BaseRepository\n{\n    protected function model() {\n        return \\App\\User::class;\n    }\n    \n    public function getAllUser(){\n        return $this-\u003eall();\n    }\n    \n    public function getByName($name) {\n        return $this-\u003ewhere(\"name\", $name)-\u003eget();\n    }\n    \n    // You can create methods with partial queries\n    public function filterByProfile($profile) {\n        return $this-\u003ewhere(\"profile\", $profile);\n    }\n    \n    // Them you can use the partial queries into your repositories\n    public function getAdmins() {\n        return $this-\u003efilterByProfile(\"admin\")-\u003eget();\n    }\n    public function getEditors() {\n        return $this-\u003efilterByProfile(\"editor\")-\u003eget();\n    }\n    \n    // You can also use Eager Loading in queries\n    public function getWithPosts() {\n        return $this-\u003ewith(\"posts\")-\u003eget();\n    }\n}\n```\nTo use the class, just inject them into the controllers.\n```php\nnamespace App\\Http\\Controllers;\n\nuse App\\Http\\Controllers\\Controller;\nuse App\\Repositories\\UserRepository;\nclass UserController extends Controller\n{\n    protected function index(UserRepository $repository) {\n        return $repository-\u003egetAdmins();\n    }\n}\n```\nThe injection can also be done in the constructor to use the repository in all methods.\n```php\nnamespace App\\Http\\Controllers;\n\nuse App\\Http\\Controllers\\Controller;\nuse App\\Repositories\\UserRepository;\nclass UserController extends Controller\n{\n    private $repository;\n\tpublic function __construct()(UserRepository $repository) {\n        $this-\u003erepository = $repository;\n    }\n    \n    public function index() {\n        return $this-\u003erepository-\u003egetAllUsers();\n    }\n    \n}\n```\n\u003e The Eloquent/QueryBuilder methods are encapsulated as protected and are available just into the repository class. Declare your own public data access methods within the repository to access them through the controller.\n\n#### Paginate\nAs default value, the `paginate` method uses, *15* records per page. This default value can be set in the configuration file to be used in all repositories.\nIf necessary, you can change the default value for a single repository overwriting the `perPage` property with the desired value.\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nclass UserRepository extends BaseRepository\n{\n    protected $perPage = 10;\n    protected function model() {\n        return \\App\\User::class;\n    }\n}\n```\n#### OrderBy\nYou can declare a field and a default direction to be used for all queries in a particular repository.\nYou can still choose other fields to sort or just skip the sorting methods.\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nclass UserRepository extends BaseRepository\n{\n    protected $orderBy = \"created_at\";\n    protected $orderByDirection = \"DESC\";\n\tprotected function model() {\n        return \\App\\User::class;\n    }\n    \n    public function getAllUser(){\n        // This query will use the default ordering of the repository.\n        return $this-\u003eall();\n    }\n    \n    public function getByName($name) {\n        // In this query only the declared sort will be used.\n        return $this-\u003eorderBy(\"name\")-\u003ewhere(\"name\", $name)-\u003eget();\n    }\n    \n    // É possível criar métodos com consultas parciais\n    public function getWithoutOrder() {\n        // In this query, no sort will be used.\n        return $this-\u003eskipOrderBy()-\u003eall();\n    }\n    \n}\n```\n#### GlobalScope\nYou can set a scope to use for all queries used in the repository.\nIf necessary, you can also ignore this global scope.\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nclass AdminRepository extends BaseRepository\n{\n    protected function model() {\n        return \\App\\User::class;\n    }\n    protected function globalScope() {\n        return $this-\u003ewhere('is_admin', true);\n    }\n    \n    public function getAdmins() {\n        // In this query the declared global scope will be included.\n        return $this-\u003eall();\n    }\n    \n    public function getAll() {\n        // In this query the declared global scope will not be included.\n        return $this-\u003eskipGlobalScope()-\u003eall();\n    }\n}\n```\n## Cache\nThe package comes with a powerful cache driver. The idea is that once the query is done, it will be cached. After the cache is done, it is possible to reduce the number of accesses to the database to zero.\nAll caching is performed using the cache driver configured for the application.\nTo use the driver, just use the trait that implements it.\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nuse GiordanoLima\\EloquentRepository\\CacheableRepository;\nclass UserRepository extends BaseRepository\n{\n    use CacheableRepository;\n    protected function model() {\n        return \\App\\User::class;\n    }\n}\n```\nUsage remains the same, with all cache management logic done automatically.\n```php\nnamespace App\\Http\\Controllers;\n\nuse App\\Http\\Controllers\\Controller;\nuse App\\Repositories\\UserRepository;\nclass UserController extends Controller\n{\n    private $repository;\n\tpublic function __construct()(UserRepository $repository) {\n        $this-\u003erepository = $repository;\n    }\n    \n    public function index() {\n        $users = $this-\u003erepository-\u003egetAllUsers();\n        // if you call the same query later (even in other requests)\n        // the query is already cached and you do not need to access the database again.\n        $users = $this-\u003erepository-\u003egetAllUsers(); \n    }\n    \n}\n```\nEverytime the database data is changed, the cache is automatically cleaned to avoid outdated data.\nHowever, if it is necessary to clear the cache, it can be performed through the `clearCache()` method.\nYou can also force access to the database and avoid cached data by using the `skipCache()` method.\n```php\nnamespace App\\Repositories;\nuse GiordanoLima\\EloquentRepository\\BaseRepository;\nuse GiordanoLima\\EloquentRepository\\CacheableRepository;\nclass UserRepository extends BaseRepository\n{\n    use CacheableRepository;\n\tprotected function model() {\n        return \\App\\User::class;\n    }\n    \n    public function createUser($data) {\n        // After inserting the data, the cache\n        // is cleaned automatically.\n        return $this-\u003ecreate($data);\n    }\n    \n    public function addRules($user, $rules) {\n        $user-\u003erules()-\u003eattach($rules);\n        $this-\u003eclearCache(); // Forcing cache cleaning.\n    }\n    \n    public function getWithoutCache() {\n        $this-\u003eskipCache()-\u003eall(); // Finding the data without using the cache.\n    }\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiordanolima%2Feloquent-repository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiordanolima%2Feloquent-repository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiordanolima%2Feloquent-repository/lists"}