{"id":17160833,"url":"https://github.com/korridor/laravel-computed-attributes","last_synced_at":"2025-04-13T13:31:42.131Z","repository":{"id":36467947,"uuid":"226346821","full_name":"korridor/laravel-computed-attributes","owner":"korridor","description":"Laravel package that adds computed attributes to eloquent models. A computed attribute is an accessor were the computed value is saved in the database.","archived":false,"fork":false,"pushed_at":"2024-03-06T11:07:59.000Z","size":42,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-04-26T09:22:05.873Z","etag":null,"topics":["accessor","caching","database","eloquent","eloquent-models","laravel","model","package","performance"],"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/korridor.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"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":"2019-12-06T14:28:40.000Z","updated_at":"2022-09-30T13:20:13.000Z","dependencies_parsed_at":"2024-10-14T22:26:14.080Z","dependency_job_id":"72ffeea4-392b-4473-985b-5d102372d8b9","html_url":"https://github.com/korridor/laravel-computed-attributes","commit_stats":{"total_commits":17,"total_committers":2,"mean_commits":8.5,"dds":0.05882352941176472,"last_synced_commit":"edcb471ce77d4c1225b96ae42f96303fa8040551"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-computed-attributes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-computed-attributes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-computed-attributes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-computed-attributes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/korridor","download_url":"https://codeload.github.com/korridor/laravel-computed-attributes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248721121,"owners_count":21151050,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["accessor","caching","database","eloquent","eloquent-models","laravel","model","package","performance"],"created_at":"2024-10-14T22:26:12.045Z","updated_at":"2025-04-13T13:31:37.120Z","avatar_url":"https://github.com/korridor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel computed attributes\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/korridor/laravel-computed-attributes?style=flat-square)](https://packagist.org/packages/korridor/laravel-computed-attributes)\n[![License](https://img.shields.io/packagist/l/korridor/laravel-computed-attributes?style=flat-square)](license.md)\n[![Supported PHP versions](https://img.shields.io/packagist/php-v/korridor/laravel-computed-attributes?style=flat-square)](https://packagist.org/packages/korridor/laravel-computed-attributes)\n[![GitHub Workflow Lint](https://img.shields.io/github/actions/workflow/status/korridor/laravel-computed-attributes/lint.yml?label=lint\u0026style=flat-square)](https://github.com/korridor/laravel-computed-attributes/actions/workflows/lint.yml)\n[![GitHub Workflow Tests](https://img.shields.io/github/actions/workflow/status/korridor/laravel-computed-attributes/unittests.yml?label=tests\u0026style=flat-square)](https://github.com/korridor/laravel-computed-attributes/actions/workflows/unittests.yml)\n[![Codecov](https://img.shields.io/codecov/c/github/korridor/laravel-computed-attributes?style=flat-square)](https://codecov.io/gh/korridor/laravel-computed-attributes)\n\nLaravel package that adds computed attributes to eloquent models.\nA computed attribute is an accessor where the value is saved in the database.\nThe value can be regenerated or validated at any time.\nThis can increase performance (no calculation at every get/fetch) and it can simplify querying the database (f.e. complex filter system). \n\n\u003e [!NOTE]\n\u003e Check out **solidtime - The modern Open Source Time-Tracker** at [solidtime.io](https://www.solidtime.io)\n\n## Installation\n\nYou can install the package via composer with following command:\n\n```bash\ncomposer require korridor/laravel-computed-attributes\n```\n\nIf you want to use this package with older Laravel/PHP version please install the 2.2.* version.\n\n```bash\ncomposer require korridor/laravel-computed-attributes \"^2.2\"\n```\n\nYou can also publish the config file to change the default configuration (e.g. the model folder path).\n\n```bash\nphp artisan vendor:publish --tag=computed-attributes-config\n```\n\n### Requirements\n\nThis package is tested for the following Laravel and PHP versions:\n\n - 10.* (PHP 8.1, 8.2, 8.3)\n - 11.* (PHP 8.2, 8.3)\n \n## Usage examples\n\nHere is an example of two computed attributes `complex_calculation` and `sum_of_votes`.\nThe functions `getComplexCalculationComputed` and `getSumOfVotesComputed` are calculating the computed attributes.\n\n```php\nuse Korridor\\LaravelComputedAttributes\\ComputedAttributes;\n\nclass Post {\n\n    use ComputedAttributes;\n\n    /**\n     * The attributes that are computed. (f.e. for performance reasons)\n     * These attributes can be regenerated at any time.\n     *\n     * @var string[]\n     */\n    protected $computed = [\n        'complex_calculation',\n        'sum_of_votes',\n    ];\n\n    /*\n     * Computed attributes.\n     */\n\n    /**\n     * @return int\n     */\n    public function getComplexCalculationComputed(): int\n    {\n        return 1 + 2;\n    }\n\n    /**\n     * @return int\n     */\n    public function getSumOfVotesComputed(): int\n    {\n        return $this-\u003evotes-\u003esum('rating');\n    }\n    \n    // ...\n}\n```\n\n\n\nhttps://laravel.com/docs/8.x/eloquent#events\n\n```php\n/**\n * Boot function from laravel.\n */\nprotected static function boot(): void\n{\n    static::saving(function (Post $model) {\n        $model-\u003esetComputedAttributeValue('sum_of_votes');\n    });\n    parent::boot();\n}\n```\n\nFor the whole code of this very simple example see the `tests/TestEnvironment` folder.\n\n## Commands\n\n### computed-attributes:generate\n\n```\ncomputed-attributes:generate { modelsAttributes? } { --chunkSize=500 } { --chunk= }\n```\n\nThis command (re-)calculates the values of the computed attributes and saves the new value.\n\n#### Query optimization\n\nYou can use the `computedAttributesGenerate` scope in any model using the `ComputedAttributes` trait to extend the query that fetches the models for the calculation.\n\n```php\nuse Illuminate\\Database\\Eloquent\\Builder;\n\n// ...\n\n/**\n * This scope will be applied during the computed property generation with artisan computed-attributes:generate.\n *\n * @param Builder $builder\n * @param array $attributes Attributes that will be generated.\n * @return Builder\n */\npublic function scopeComputedAttributesGenerate(Builder $builder, array $attributes): Builder\n{\n    if (in_array('sum_of_votes', $attributes)) {\n        return $builder-\u003ewith('votes');\n    }\n\n    return $builder;\n}\n```\n\n### computed-attributes:validate\n\n```\nartisan computed-attributes:validate { modelsAttributes? } { --chunkSize=500 } { --chunk= }\n```\n\nThis command validates the current values of the computed attributes.\n\n#### Query optimization\n\n```php\nuse Illuminate\\Database\\Eloquent\\Builder;\n\n// ...\n\n/**\n * This scope will be applied during the computed property validation with artisan computed-attributes:validate.\n *\n * @param Builder $builder\n * @param array $attributes Attributes that will be validated.\n * @return Builder\n */\npublic function scopeComputedAttributesValidate(Builder $builder, array $attributes): Builder\n{\n    if (in_array('sum_of_votes', $attributes)) {\n        return $builder-\u003ewith('votes');\n    }\n\n    return $builder;\n}\n```\n\n## Contributing\n\nI am open for suggestions and contributions. Just create an issue or a pull request.\n\n### Local docker environment\n\nThe `docker` folder contains a local docker environment for development.\nThe docker workspace has composer and xdebug installed.\n\n```bash\ndocker-compose run workspace bash\n```\n\n### Testing\n\nThe `composer test` command runs all tests with [phpunit](https://phpunit.de/).\nThe `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.\n\n### Codeformatting/Linting\n\nThe `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).\nThe `composer lint` command checks the code with [phpcs](https://github.com/squizlabs/PHP_CodeSniffer).\n\n## License\n\nThis package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorridor%2Flaravel-computed-attributes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkorridor%2Flaravel-computed-attributes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorridor%2Flaravel-computed-attributes/lists"}