PHPackages                             n7olkachev/laravel-computed-properties - 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. n7olkachev/laravel-computed-properties

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

n7olkachev/laravel-computed-properties
======================================

Computed properties for Eloquent

v1.1.3(7y ago)1886079MITPHPPHP ^7.0

Since Aug 22Pushed 7y ago4 watchersCompare

[ Source](https://github.com/n7olkachev/laravel-computed-properties)[ Packagist](https://packagist.org/packages/n7olkachev/laravel-computed-properties)[ Docs](https://github.com/n7olkachev/laravel-computed-properties)[ RSS](/packages/n7olkachev-laravel-computed-properties/feed)WikiDiscussions master Synced 4w ago

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

Computed properties for Eloquent
================================

[](#computed-properties-for-eloquent)

[![](https://camo.githubusercontent.com/bf145b0037e1d79cc706e3e3d9004c734fb81733ffe45a2e4e431ddb91ed79dd/68747470733a2f2f7765627365637265742e62792f696d616765732f6c6f676f2d6769746875622e706e67)](https://websecret.by)

[![Code quality](https://camo.githubusercontent.com/6a09dd078c1aae132edb1a0e5125d1acc92243935f1a292e7c904269a023ac48/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6e376f6c6b61636865762f6c61726176656c2d636f6d70757465642d70726f706572746965732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/n7olkachev/laravel-computed-properties/)[![Latest Version on Packagist](https://camo.githubusercontent.com/cf672224e7c5497b9ff7622bb64bc0e658670175c2705061ccda3b79ef605bd8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e376f6c6b61636865762f6c61726176656c2d636f6d70757465642d70726f706572746965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/n7olkachev/laravel-computed-properties)[![Licence](https://camo.githubusercontent.com/97de5c69bb38f9150c34e4ccff5ec1fa4e39296102117572b577872f457c999e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6e376f6c6b61636865762f6c61726176656c2d636f6d70757465642d70726f706572746965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/n7olkachev/laravel-computed-properties)[![Build Status](https://camo.githubusercontent.com/a9fd15de7ebdd83ea19e9417105689f2f434984d4348bc976688f7cdcafff89d/68747470733a2f2f7472617669732d63692e6f72672f6e376f6c6b61636865762f6c61726176656c2d636f6d70757465642d70726f706572746965732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/n7olkachev/laravel-computed-properties)

**Laravel 5.4+**

Based on this tweet:

Some examples for better understanding of this power:

```
class Order extends Model
{
    use ComputedProperties;

    public function products()
    {
        return $this->hasMany(OrderProduct::class);
    }

    public function computedSum($order)
    {
        return OrderProduct::select(new Expression('sum(price * count)'))
            ->where('order_id', $order->id);
    }
}
```

Now, we can get order sum with `$order->sum`. Yep, we can get this functionality with `getSumAttribute` but wait! The real power of this package is that we can use this method inside our queries:

```
$orders = Order::withComputed('sum')->get()
```

We eager loaded `sum` attribute without `N+1` problem.

But there is more! You can add `having` or `orderBy` clauses to such queries for filtering and sorting!

```
Order::withComputed('sum')->orderBy('sum', 'desc')->get()
```

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

[](#installation)

You can install the package via composer:

```
composer require n7olkachev/laravel-computed-properties
```

Next, add ComputedProperties trait to your models:

```
use ComputedProperties;
```

That's all!

More examples
-------------

[](#more-examples)

```
class Page extends Model
{
    use ComputedProperties;

    public $timestamps = false;

    protected $casts = [
        'last_view' => 'datetime',
        'first_view' => 'datetime',
    ];

    public function computedLastView($page)
    {
        return PageView::select(new Expression('max(viewed_at)'))
            ->where('page_id', $page->id);
    }

    public function computedFirstView($page)
    {
        return PageView::select(new Expression('min(viewed_at)'))
            ->where('page_id', $page->id);
    }
}
```

We can find `Page` by its first view:

```
$page = Page::withComputed('first_view')
    ->having('first_view', Carbon::create(2017, 8, 16, 0, 0, 0))
    ->first();
```

Or by both `first_view` and `last_view`

```
$page = Page::withComputed(['first_view', 'last_view'])
    ->having('first_view', Carbon::create(2017, 8, 16, 0, 0, 0))
    ->having('last_view', Carbon::create(2017, 8, 21, 0, 0, 0))
    ->first();
```

We can order pages by theirs `last_view`

```
$pages = Page::withComputed('last_view')
    ->orderBy('last_view', 'desc')
    ->get()
```

Testing
-------

[](#testing)

```
$ composer test
```

Credits
-------

[](#credits)

- [Jonathan Reinink](https://github.com/reinink) (idea)
- [Nikita Tolkachev](https://github.com/n7olkachev)

Sponsored by
------------

[](#sponsored-by)

Web agency based in Minsk, Belarus

License
-------

[](#license)

The MIT License (MIT)

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 86.4% 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 ~101 days

Total

5

Last Release

2874d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b1dde1980432378072b5f61d495c578baeaa3ac833dd16e99a2db03c642e4a9a?d=identicon)[n7olkachev](/maintainers/n7olkachev)

---

Top Contributors

[![n7olkachev](https://avatars.githubusercontent.com/u/3993345?v=4)](https://github.com/n7olkachev "n7olkachev (19 commits)")[![ankurk91](https://avatars.githubusercontent.com/u/6111524?v=4)](https://github.com/ankurk91 "ankurk91 (2 commits)")[![tabuna](https://avatars.githubusercontent.com/u/5102591?v=4)](https://github.com/tabuna "tabuna (1 commits)")

---

Tags

eloquentlaravellaraveleloquentpropertiescomputedwebsecret

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/n7olkachev-laravel-computed-properties/health.svg)

```
[![Health](https://phpackages.com/badges/n7olkachev-laravel-computed-properties/health.svg)](https://phpackages.com/packages/n7olkachev-laravel-computed-properties)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k8.9M109](/packages/mongodb-laravel-mongodb)[api-platform/laravel

API Platform support for Laravel

58190.1k21](/packages/api-platform-laravel)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k35.7M51](/packages/kirschbaum-development-eloquent-power-joins)[mike-bronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k161.4k2](/packages/mike-bronner-laravel-model-caching)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)

PHPackages © 2026

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