PHPackages                             astrotomic/laravel-cachable-attributes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. astrotomic/laravel-cachable-attributes

ActiveLibrary[Database &amp; ORM](/categories/database)

astrotomic/laravel-cachable-attributes
======================================

Allows to cache attribute accessor values in an easy way.

0.5.0(3y ago)3240.0k↓10.7%4[1 issues](https://github.com/Astrotomic/laravel-cachable-attributes/issues)[1 PRs](https://github.com/Astrotomic/laravel-cachable-attributes/pulls)MITPHPPHP ^7.2 || ^8.0CI failing

Since Aug 30Pushed 2y ago2 watchersCompare

[ Source](https://github.com/Astrotomic/laravel-cachable-attributes)[ Packagist](https://packagist.org/packages/astrotomic/laravel-cachable-attributes)[ Docs](https://astrotomic.info)[ Fund](https://forest.astrotomic.info)[ GitHub Sponsors](https://github.com/Gummibeer)[ RSS](/packages/astrotomic-laravel-cachable-attributes/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (6)Versions (7)Used By (0)

Laravel cachable Attributes
===========================

[](#laravel-cachable-attributes)

[![Latest Version](https://camo.githubusercontent.com/bf0e0beb03bf1090b3a090e833be9c15395f093bf0a1e4da716f1a248b4bb1f8/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f617374726f746f6d69632f6c61726176656c2d6361636861626c652d617474726962757465732e7376673f6c6162656c3d52656c65617365267374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/astrotomic/laravel-cachable-attributes)[![MIT License](https://camo.githubusercontent.com/0ed2f0d806588e7e61ff14df547f34257f06fcdb1ad53bb82193db5331f956da/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f417374726f746f6d69632f6c61726176656c2d6361636861626c652d617474726962757465732e7376673f6c6162656c3d4c6963656e736526636f6c6f723d626c7565267374796c653d666f722d7468652d6261646765)](https://github.com/Astrotomic/laravel-cachable-attributes/blob/master/LICENSE)[![Offset Earth](https://camo.githubusercontent.com/d204555ebe1fb0ae82d10c97b4f4ffc2dfdd2ba1489f98be7f7e8708333a0466/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d677265656e3f7374796c653d666f722d7468652d6261646765)](https://plant.treeware.earth/Astrotomic/laravel-cachable-attributes)[![Larabelles](https://camo.githubusercontent.com/a2c8d5126ddd8c5ddc627176d1d2e0568f8399b50038e71fd7f774c3e24dbe4b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726162656c6c65732d2546302539462541362538342d6c6967687470696e6b3f7374796c653d666f722d7468652d6261646765)](https://www.larabelles.com/)

[![Total Downloads](https://camo.githubusercontent.com/204b46a618318f76689b908faee4467154c5fea78eb962df8d955d028e24af06/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617374726f746f6d69632f6c61726176656c2d6361636861626c652d617474726962757465732e7376673f6c6162656c3d446f776e6c6f616473267374796c653d666c61742d7371756172652663616368655365636f6e64733d363030)](https://packagist.org/packages/astrotomic/laravel-cachable-attributes)[![GitHub Workflow Status](https://camo.githubusercontent.com/cd2de7aea940d2132ede32f59b32bde3dbd67c8bbc329d673d38213f116394d3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f417374726f746f6d69632f6c61726176656c2d6361636861626c652d617474726962757465732f72756e2d74657374733f7374796c653d666c61742d737175617265266c6f676f436f6c6f723d7768697465266c6f676f3d676974687562266c6162656c3d5465737473)](https://github.com/Astrotomic/laravel-cachable-attributes/actions?query=workflow%3Arun-tests)[![StyleCI](https://camo.githubusercontent.com/ac89560c9a5957068f20c383861eafb27da01e397b20a77f311260c2d2a68f59/68747470733a2f2f7374796c6563692e696f2f7265706f732f3230353136373132382f736869656c64)](https://styleci.io/repos/205167128)

**If you want to cache your heavy attribute accessors - this package is for you!**

This Laravel package provides a trait to use in your models which provides methods to cache your complex, long running, heavy model accessor results.

Installation
------------

[](#installation)

You just have to run `composer require astrotomic/laravel-cachable-attributes`. There's no ServiceProvider or config or anything else.

Quick Example
-------------

[](#quick-example)

Sometimes 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.

```
class Gallery extends Model
{
    public function images(): HasMany
    {
        return $this->hasMany(Image::class, 'gallery_id');
    }

    public function getStorageSizeAttribute(): int
    {
        return $this->images()->sum('file_size');
    }
}
```

This example would run the sum query every time you access `$gallery->storage_size`. By using the trait you can prevent this.

```
use Astrotomic\CachableAttributes\CachableAttributes;
use Astrotomic\CachableAttributes\CachesAttributes;

class Gallery extends Model implements CachableAttributes
{
    use CachesAttributes;

    protected $cachableAttributes = [
        'storage_size',
    ];

    public function images(): HasMany
    {
        return $this->hasMany(Image::class, 'gallery_id');
    }

    public function getStorageSizeAttribute(): int
    {
        return $this->remember('storage_size', 0, function(): int {
            return $this->images()->sum('file_size');
        });
    }
}
```

This 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.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please 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).

### Security

[](#security)

If you discover any security related issues, please check [SECURITY](https://github.com/Astrotomic/.github/blob/master/SECURITY.md) for steps to report it.

Credits
-------

[](#credits)

- [Tom Witkowski](https://github.com/Gummibeer)
- [All Contributors](https://github.com/Astrotomic/laravel-cachable-attributes/graphs/contributors)

Treeware
--------

[](#treeware)

You'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.

It’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.

You can buy trees at [offset.earth/treeware](https://plant.treeware.earth/Astrotomic/laravel-cachable-attributes)

Read more about Treeware at [treeware.earth](https://treeware.earth)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~333 days

Total

5

Last Release

1119d ago

PHP version history (2 changes)0.1.0PHP ^7.2

0.3.0PHP ^7.2 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6187884?v=4)[Tom Herrmann](/maintainers/Gummibeer)[@Gummibeer](https://github.com/Gummibeer)

---

Top Contributors

[![Gummibeer](https://avatars.githubusercontent.com/u/6187884?v=4)](https://github.com/Gummibeer "Gummibeer (64 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")[![osbre](https://avatars.githubusercontent.com/u/23292709?v=4)](https://github.com/osbre "osbre (1 commits)")

---

Tags

accessorcacheeloquenthacktoberfestlaravellaravel-cachable-attributestraittreewarelaravelmodeleloquentcacheaccessorgetterattribute

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/astrotomic-laravel-cachable-attributes/health.svg)

```
[![Health](https://phpackages.com/badges/astrotomic-laravel-cachable-attributes/health.svg)](https://phpackages.com/packages/astrotomic-laravel-cachable-attributes)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[silber/bouncer

Eloquent roles and abilities.

3.6k4.4M25](/packages/silber-bouncer)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[ymigval/laravel-model-cache

Laravel package for caching Eloquent model queries

7642.2k3](/packages/ymigval-laravel-model-cache)[korridor/laravel-computed-attributes

Laravel package that adds computed attributes to eloquent models. A computed attribute is an accessor were the computed value is saved in the database.

1237.6k](/packages/korridor-laravel-computed-attributes)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
