PHPackages                             sehrgut/eloquent-computed-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. sehrgut/eloquent-computed-attributes

AbandonedArchivedLibrary

sehrgut/eloquent-computed-attributes
====================================

Automatically compute attributes on Laravel Eloquent models when their input changes

0.0.2(7y ago)343.4k1[1 issues](https://github.com/sehrgutesoftware/eloquent-computed-attributes/issues)MITPHP

Since Jun 20Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sehrgutesoftware/eloquent-computed-attributes)[ Packagist](https://packagist.org/packages/sehrgut/eloquent-computed-attributes)[ RSS](/packages/sehrgut-eloquent-computed-attributes/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (3)Used By (0)

Eloquent Computed Attributes
============================

[](#eloquent-computed-attributes)

> Automatically compute attributes on Laravel Eloquent models when their input changes

*Computed attributes* provide an alternative to Laravel's built-in *accessors*. In contrast to accessors, *computed attributes* are not computed when their value is requested, but rather when their dependencies change (*computed attributes* are persisted in the DB). It should be preferred in cases of resource intensive computations.

It works by listening for a models' `saving` event and checking whether the dependencies of a *computed attribute* are dirty. If so, the attribute is recomputed and its new value is updated in the database.

Dependencies of a computed attribute are declared through the signature of the "compute" method. Argument names in that method correspond to attributes on the model. See below for an example on how this works.

**Example Use Cases:**

- Render markdown fields as html
- Save excerpts of longer texts
- Geocoding of addresses on update

**Comparison with other options:**

MethodUse CaseComputation time[Laravel Accessors](https://laravel.com/docs/5.6/eloquent-mutators#accessors-and-mutators)Simple transformations (affected by n+1 issue)On read[laravel-computed-properties](https://github.com/n7olkachev/laravel-computed-properties)Querieable attributes that should be computed on read (avoids n+1 issue).On readThis packageTime consuming operations or operations involving external API callsOn writeGetting Started
---------------

[](#getting-started)

### 1. Installation

[](#1-installation)

```
composer require sehrgut/eloquent-computed-attributes

```

### 2. Define a *computed attribute*

[](#2-define-a-computed-attribute)

Let's say we have a `Post` model with a `text` column and a `text_excerpt` column. Every time the value of `text` changes, we want to recompute the `text_excerpt` column. This is what our `Post` class could look like:

```
