PHPackages                             testmonitor/eloquent-calculated-columns - 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. testmonitor/eloquent-calculated-columns

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

testmonitor/eloquent-calculated-columns
=======================================

A Laravel package for adding calculated columns to Eloquent models using SQL for more performant queries.

v2.0.0(1y ago)0770↓50%1MITPHPPHP ^8.2

Since Nov 27Pushed 1y ago2 watchersCompare

[ Source](https://github.com/testmonitor/eloquent-calculated-columns)[ Packagist](https://packagist.org/packages/testmonitor/eloquent-calculated-columns)[ RSS](/packages/testmonitor-eloquent-calculated-columns/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (7)Versions (3)Used By (0)

Eloquent Calculated Columns
===========================

[](#eloquent-calculated-columns)

[![Latest Stable Version](https://camo.githubusercontent.com/46d2e15480aeb196848e1a8e5ca583617cc435a3231bbd84e1d8e63605590b23/68747470733a2f2f706f7365722e707567782e6f72672f746573746d6f6e69746f722f656c6f7175656e742d63616c63756c617465642d636f6c756d6e732f762f737461626c65)](https://packagist.org/packages/testmonitor/eloquent-calculated-columns)[![codecov](https://camo.githubusercontent.com/9ea77d03e11ea0d419c058db58486932960a9d528ac997fa0a5795d93a817a9e/68747470733a2f2f636f6465636f762e696f2f67682f746573746d6f6e69746f722f656c6f7175656e742d63616c63756c617465642d636f6c756d6e732f67726170682f62616467652e7376673f746f6b656e3d324a374d34464e573844)](https://codecov.io/gh/testmonitor/eloquent-calculated-columns)[![StyleCI](https://camo.githubusercontent.com/5aca250eece48c7e767587ed0c14f9a92a6e66832e9f32b92d816b189b867db0/68747470733a2f2f7374796c6563692e696f2f7265706f732f3832363238373231362f736869656c64)](https://styleci.io/repos/826287216)[![License](https://camo.githubusercontent.com/65fddcb757af9cea3a6be75319e73329ded500181384afd1810c6c6595e9f11b/68747470733a2f2f706f7365722e707567782e6f72672f746573746d6f6e69746f722f656c6f7175656e742d63616c63756c617465642d636f6c756d6e732f6c6963656e7365)](https://packagist.org/packages/eloquent-calculated-columns)

A Laravel package for adding calculated columns when retrieving data from an Eloquent model. This package allows you to define these columns using SQL, resulting in more performant queries compared to accessors.

It is heavily inspired by Spatie's [Query Builder](https://github.com/spatie/laravel-query-builder/) and can be used in conjunction with this package.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [Tests](#tests)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

This package can be installed through Composer:

```
$ composer require testmonitor/eloquent-calculated-columns
```

Next, publish the configuration file:

```
$ php artisan vendor:publish --tag=eloquent-calculated-columns
```

The configuration file allows you the change the HTTP parameter name, when desired.

Usage
-----

[](#usage)

To use calculated columns, you need to:

1. Use the trait `TestMonitor\CalculatedColumns\HasCalculatedColumns` in your model.
2. Define the available calculated in your model.

Add the CalculatedColumns trait to the models where you want to add calculated columns:

```
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
use TestMonitor\CalculatedColumns\HasCalculatedColumns;

class User extends Model
{
    use HasCalculatedColumns;

    public function calculatedColumns(): array
    {
        return [
            'total_price' => function (Builder $query) {
                $query->select(DB::raw("SUM(order_items.price) AS total_price"))
                    ->from('order_items')
                    ->whereColumn('order_items.order_id', 'orders.id');
            },
        ];
    }
}
```

Next, use the calculated columns in your queries:

```
use App\Models\Order;

$orders = Order::query()
    ->withCalculatedColumns()
    ->get();
}
```

In this example, the total\_price column calculates the total price of an order by adding all the order item prices.

The requested columns are automatically derived from the HTTP request. You can modify the HTTP query parameter in the configuration file. By default, the name `calculate` is used.

Examples
--------

[](#examples)

Tests
-----

[](#tests)

The package contains integration tests. You can run them using PHPUnit.

```
$ vendor/bin/phpunit

```

Changelog
---------

[](#changelog)

Refer to [CHANGELOG](CHANGELOG.md) for more information.

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

[](#contributing)

Refer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

Credits
-------

[](#credits)

- [Thijs Kok](https://www.testmonitor.com/)
- [Stephan Grootveld](https://www.testmonitor.com/)
- [Frank Keulen](https://www.testmonitor.com/)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Refer to the [License](LICENSE.md) for more information.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance47

Moderate activity, may be stable

Popularity18

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 83.3% 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 ~133 days

Total

2

Last Release

396d ago

Major Versions

v1.0.0 → v2.0.02025-04-09

### Community

Maintainers

![](https://www.gravatar.com/avatar/39f48c881813b7d3b044ca5660aa5ab9e60b5dd7c34ed4a47acbb11bd20b7593?d=identicon)[thijskok](/maintainers/thijskok)

---

Top Contributors

[![thijskok](https://avatars.githubusercontent.com/u/1344550?v=4)](https://github.com/thijskok "thijskok (10 commits)")[![stefanius](https://avatars.githubusercontent.com/u/2707905?v=4)](https://github.com/stefanius "stefanius (1 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

---

Tags

laravelmodeleloquenttestmonitorcalculated columns

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/testmonitor-eloquent-calculated-columns/health.svg)

```
[![Health](https://phpackages.com/badges/testmonitor-eloquent-calculated-columns/health.svg)](https://phpackages.com/packages/testmonitor-eloquent-calculated-columns)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[cviebrock/eloquent-sluggable

Easy creation of slugs for your Eloquent models in Laravel

4.0k13.6M253](/packages/cviebrock-eloquent-sluggable)[dyrynda/laravel-model-uuid

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

4802.8M8](/packages/dyrynda-laravel-model-uuid)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)[spiritix/lada-cache

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

591444.8k2](/packages/spiritix-lada-cache)

PHPackages © 2026

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