https://github.com/testmonitor/eloquent-calculated-columns
A Laravel package for adding calculated columns to Eloquent models using SQL for more performant queries.
https://github.com/testmonitor/eloquent-calculated-columns
Last synced: about 2 months ago
JSON representation
A Laravel package for adding calculated columns to Eloquent models using SQL for more performant queries.
- Host: GitHub
- URL: https://github.com/testmonitor/eloquent-calculated-columns
- Owner: testmonitor
- License: mit
- Created: 2024-07-09T12:15:08.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-09T11:22:09.000Z (about 1 year ago)
- Last Synced: 2025-10-24T22:56:52.667Z (6 months ago)
- Language: PHP
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Eloquent Calculated Columns
[](https://packagist.org/packages/testmonitor/eloquent-calculated-columns)
[](https://codecov.io/gh/testmonitor/eloquent-calculated-columns)
[](https://styleci.io/repos/826287216)
[](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
- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [Tests](#tests)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)
## Installation
This package can be installed through Composer:
```sh
$ composer require testmonitor/eloquent-calculated-columns
```
Next, publish the configuration file:
```sh
$ php artisan vendor:publish --tag=eloquent-calculated-columns
```
The configuration file allows you the change the HTTP parameter name, when desired.
## 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:
```php
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:
```php
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
## Tests
The package contains integration tests. You can run them using PHPUnit.
```
$ vendor/bin/phpunit
```
## Changelog
Refer to [CHANGELOG](CHANGELOG.md) for more information.
## Contributing
Refer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details.
## Credits
- [Thijs Kok](https://www.testmonitor.com/)
- [Stephan Grootveld](https://www.testmonitor.com/)
- [Frank Keulen](https://www.testmonitor.com/)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Refer to the [License](LICENSE.md) for more information.