{"id":18578280,"url":"https://github.com/astrotomic/laravel-cachable-attributes","last_synced_at":"2025-07-08T03:32:51.009Z","repository":{"id":35090360,"uuid":"205167128","full_name":"Astrotomic/laravel-cachable-attributes","owner":"Astrotomic","description":"Allows to cache attribute accessor values in an easy way.","archived":false,"fork":false,"pushed_at":"2024-03-02T07:00:10.000Z","size":72,"stargazers_count":31,"open_issues_count":2,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-02T02:23:29.081Z","etag":null,"topics":["accessor","cache","eloquent","hacktoberfest","laravel","laravel-cachable-attributes","trait","treeware"],"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/Astrotomic.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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},"funding":{"github":["Gummibeer","SarahSibert"],"issuehunt":"astrotomic","custom":["https://forest.astrotomic.info"]}},"created_at":"2019-08-29T13:11:36.000Z","updated_at":"2024-06-25T14:52:10.000Z","dependencies_parsed_at":"2022-07-08T13:30:25.422Z","dependency_job_id":"8ee498ac-8217-4bda-8aa6-a4f3e02e6e62","html_url":"https://github.com/Astrotomic/laravel-cachable-attributes","commit_stats":{"total_commits":64,"total_committers":3,"mean_commits":"21.333333333333332","dds":0.03125,"last_synced_commit":"478dade605c4a924b63daaff8b968424f659d714"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Astrotomic%2Flaravel-cachable-attributes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Astrotomic%2Flaravel-cachable-attributes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Astrotomic%2Flaravel-cachable-attributes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Astrotomic%2Flaravel-cachable-attributes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Astrotomic","download_url":"https://codeload.github.com/Astrotomic/laravel-cachable-attributes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248199136,"owners_count":21063641,"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":["accessor","cache","eloquent","hacktoberfest","laravel","laravel-cachable-attributes","trait","treeware"],"created_at":"2024-11-06T23:34:30.582Z","updated_at":"2025-04-10T10:31:00.264Z","avatar_url":"https://github.com/Astrotomic.png","language":"PHP","funding_links":["https://github.com/sponsors/Gummibeer","https://github.com/sponsors/SarahSibert","https://issuehunt.io/r/astrotomic","https://forest.astrotomic.info"],"categories":[],"sub_categories":[],"readme":"# Laravel cachable Attributes\n\n[![Latest Version](http://img.shields.io/packagist/v/astrotomic/laravel-cachable-attributes.svg?label=Release\u0026style=for-the-badge)](https://packagist.org/packages/astrotomic/laravel-cachable-attributes)\n[![MIT License](https://img.shields.io/github/license/Astrotomic/laravel-cachable-attributes.svg?label=License\u0026color=blue\u0026style=for-the-badge)](https://github.com/Astrotomic/laravel-cachable-attributes/blob/master/LICENSE)\n[![Offset Earth](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-green?style=for-the-badge)](https://plant.treeware.earth/Astrotomic/laravel-cachable-attributes)\n[![Larabelles](https://img.shields.io/badge/Larabelles-%F0%9F%A6%84-lightpink?style=for-the-badge)](https://www.larabelles.com/)\n\n[![Total Downloads](https://img.shields.io/packagist/dt/astrotomic/laravel-cachable-attributes.svg?label=Downloads\u0026style=flat-square\u0026cacheSeconds=600)](https://packagist.org/packages/astrotomic/laravel-cachable-attributes)\n[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Astrotomic/laravel-cachable-attributes/run-tests?style=flat-square\u0026logoColor=white\u0026logo=github\u0026label=Tests)](https://github.com/Astrotomic/laravel-cachable-attributes/actions?query=workflow%3Arun-tests)\n[![StyleCI](https://styleci.io/repos/205167128/shield)](https://styleci.io/repos/205167128)\n\n**If you want to cache your heavy attribute accessors - this package is for you!**\n\nThis Laravel package provides a trait to use in your models which provides methods to cache your complex, long running, heavy model accessor results.\n\n## Installation\n\nYou just have to run `composer require astrotomic/laravel-cachable-attributes`. There's no ServiceProvider or config or anything else.\n\n## Quick Example\n\nSometimes you have properties which run addition database queries, do heavy calculations or have to retrieve data from somewhere. This slows down your application and if you access the attribute multiple times the accessor is also executed multiple times.\n\n```php\nclass Gallery extends Model\n{\n    public function images(): HasMany\n    {\n        return $this-\u003ehasMany(Image::class, 'gallery_id');\n    }\n\n    public function getStorageSizeAttribute(): int\n    {\n        return $this-\u003eimages()-\u003esum('file_size');\n    }\n}\n```\n\nThis example would run the sum query every time you access `$gallery-\u003estorage_size`.\nBy using the trait you can prevent this.\n\n```php\nuse Astrotomic\\CachableAttributes\\CachableAttributes;\nuse Astrotomic\\CachableAttributes\\CachesAttributes;\n\nclass Gallery extends Model implements CachableAttributes\n{\n    use CachesAttributes;\n\n    protected $cachableAttributes = [\n        'storage_size',\n    ];\n\n    public function images(): HasMany\n    {\n        return $this-\u003ehasMany(Image::class, 'gallery_id');\n    }\n\n    public function getStorageSizeAttribute(): int\n    {\n        return $this-\u003eremember('storage_size', 0, function(): int {\n            return $this-\u003eimages()-\u003esum('file_size');\n        });\n    }\n}\n```\n\nThis will run the database query only once per request. The ttl of `0` means to cache only for the current runtime. You could also use `null` or `rememberForever()` to remember the value forever (until manually deleted). Or use any positive number to cache for the amount of seconds.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/Astrotomic/.github/blob/master/CONTRIBUTING.md) for details. You could also be interested in [CODE OF CONDUCT](https://github.com/Astrotomic/.github/blob/master/CODE_OF_CONDUCT.md).\n\n### Security\n\nIf you discover any security related issues, please check [SECURITY](https://github.com/Astrotomic/.github/blob/master/SECURITY.md) for steps to report it.\n\n## Credits\n\n- [Tom Witkowski](https://github.com/Gummibeer)\n- [All Contributors](https://github.com/Astrotomic/laravel-cachable-attributes/graphs/contributors)\n\n## Treeware\n\nYou're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.\n\nIt’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to [plant trees](https://www.bbc.co.uk/news/science-environment-48870920). If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.\n\nYou can buy trees at [offset.earth/treeware](https://plant.treeware.earth/Astrotomic/laravel-cachable-attributes)\n\nRead more about Treeware at [treeware.earth](https://treeware.earth)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastrotomic%2Flaravel-cachable-attributes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastrotomic%2Flaravel-cachable-attributes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastrotomic%2Flaravel-cachable-attributes/lists"}