{"id":21561664,"url":"https://github.com/testmonitor/eloquent-calculated-columns","last_synced_at":"2026-02-27T19:09:52.878Z","repository":{"id":247642920,"uuid":"826287216","full_name":"testmonitor/eloquent-calculated-columns","owner":"testmonitor","description":"A Laravel package for adding calculated columns to Eloquent models using SQL for more performant queries.","archived":false,"fork":false,"pushed_at":"2025-04-09T11:22:09.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-24T22:56:52.667Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/testmonitor.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-09T12:15:08.000Z","updated_at":"2025-04-09T11:21:48.000Z","dependencies_parsed_at":"2025-04-09T12:23:26.365Z","dependency_job_id":null,"html_url":"https://github.com/testmonitor/eloquent-calculated-columns","commit_stats":null,"previous_names":["testmonitor/eloquent-calculated-columns"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/testmonitor/eloquent-calculated-columns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Feloquent-calculated-columns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Feloquent-calculated-columns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Feloquent-calculated-columns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Feloquent-calculated-columns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/testmonitor","download_url":"https://codeload.github.com/testmonitor/eloquent-calculated-columns/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testmonitor%2Feloquent-calculated-columns/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29909328,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T17:28:36.873Z","status":"ssl_error","status_checked_at":"2026-02-27T17:28:20.970Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T09:27:31.138Z","updated_at":"2026-02-27T19:09:52.852Z","avatar_url":"https://github.com/testmonitor.png","language":"PHP","readme":"# Eloquent Calculated Columns\n\n[![Latest Stable Version](https://poser.pugx.org/testmonitor/eloquent-calculated-columns/v/stable)](https://packagist.org/packages/testmonitor/eloquent-calculated-columns)\n[![codecov](https://codecov.io/gh/testmonitor/eloquent-calculated-columns/graph/badge.svg?token=2J7M4FNW8D)](https://codecov.io/gh/testmonitor/eloquent-calculated-columns)\n[![StyleCI](https://styleci.io/repos/826287216/shield)](https://styleci.io/repos/826287216)\n[![License](https://poser.pugx.org/testmonitor/eloquent-calculated-columns/license)](https://packagist.org/packages/eloquent-calculated-columns)\n\nA 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.\n\nIt is heavily inspired by Spatie's [Query Builder](https://github.com/spatie/laravel-query-builder/) and can be used in conjunction with this package.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Examples](#examples)\n- [Tests](#tests)\n- [Changelog](#changelog)\n- [Contributing](#contributing)\n- [Credits](#credits)\n- [License](#license)\n\n## Installation\n\nThis package can be installed through Composer:\n\n```sh\n$ composer require testmonitor/eloquent-calculated-columns\n```\n\nNext, publish the configuration file:\n\n```sh\n$ php artisan vendor:publish --tag=eloquent-calculated-columns\n```\n\nThe configuration file allows you the change the HTTP parameter name, when desired.\n\n## Usage\n\nTo use calculated columns, you need to:\n\n1. Use the trait ```TestMonitor\\CalculatedColumns\\HasCalculatedColumns``` in your model.\n2. Define the available calculated in your model.\n\nAdd the CalculatedColumns trait to the models where you want to add calculated columns:\n\n```php\nuse Illuminate\\Database\\Query\\Builder;\nuse Illuminate\\Database\\Eloquent\\Model;\nuse TestMonitor\\CalculatedColumns\\HasCalculatedColumns;\n\nclass User extends Model\n{\n    use HasCalculatedColumns;\n\n    public function calculatedColumns(): array\n    {\n        return [\n            'total_price' =\u003e function (Builder $query) {\n                $query-\u003eselect(DB::raw(\"SUM(order_items.price) AS total_price\"))\n                    -\u003efrom('order_items')\n                    -\u003ewhereColumn('order_items.order_id', 'orders.id');\n            },\n        ];\n    }\n}\n```\n\nNext, use the calculated columns in your queries:\n\n```php\nuse App\\Models\\Order;\n\n$orders = Order::query()\n    -\u003ewithCalculatedColumns()\n    -\u003eget();\n}\n```\n\nIn this example, the total_price column calculates the total price of an order\nby adding all the order item prices.\n\nThe requested columns are automatically derived from the HTTP request. You can\nmodify the HTTP query parameter in the configuration file. By default,\nthe name `calculate` is used.\n\n## Examples\n\n## Tests\n\nThe package contains integration tests. You can run them using PHPUnit.\n\n```\n$ vendor/bin/phpunit\n```\n\n## Changelog\n\nRefer to [CHANGELOG](CHANGELOG.md) for more information.\n\n## Contributing\n\nRefer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details.\n\n## Credits\n\n- [Thijs Kok](https://www.testmonitor.com/)\n- [Stephan Grootveld](https://www.testmonitor.com/)\n- [Frank Keulen](https://www.testmonitor.com/)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Refer to the [License](LICENSE.md) for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestmonitor%2Feloquent-calculated-columns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftestmonitor%2Feloquent-calculated-columns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestmonitor%2Feloquent-calculated-columns/lists"}