{"id":21181569,"url":"https://github.com/n7olkachev/laravel-computed-properties","last_synced_at":"2025-04-09T13:05:39.970Z","repository":{"id":57023120,"uuid":"101068564","full_name":"n7olkachev/laravel-computed-properties","owner":"n7olkachev","description":"Make your accessors smarter","archived":false,"fork":false,"pushed_at":"2018-10-02T07:36:51.000Z","size":14,"stargazers_count":191,"open_issues_count":0,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-02T11:05:52.591Z","etag":null,"topics":["eloquent","laravel"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/n7olkachev.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-22T13:54:57.000Z","updated_at":"2025-02-11T21:33:31.000Z","dependencies_parsed_at":"2022-08-23T14:00:58.861Z","dependency_job_id":null,"html_url":"https://github.com/n7olkachev/laravel-computed-properties","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n7olkachev%2Flaravel-computed-properties","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n7olkachev%2Flaravel-computed-properties/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n7olkachev%2Flaravel-computed-properties/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n7olkachev%2Flaravel-computed-properties/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n7olkachev","download_url":"https://codeload.github.com/n7olkachev/laravel-computed-properties/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045231,"owners_count":21038553,"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":["eloquent","laravel"],"created_at":"2024-11-20T17:51:24.516Z","updated_at":"2025-04-09T13:05:39.945Z","avatar_url":"https://github.com/n7olkachev.png","language":"PHP","funding_links":[],"categories":["Packages"],"sub_categories":["Database/Eloquent/Models"],"readme":"# Computed properties for Eloquent\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://websecret.by\"\u003e\u003cimg src=\"https://websecret.by/images/logo-github.png\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n[![Code quality](https://img.shields.io/scrutinizer/g/n7olkachev/laravel-computed-properties.svg?style=flat-square)](https://scrutinizer-ci.com/g/n7olkachev/laravel-computed-properties/)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/n7olkachev/laravel-computed-properties.svg?style=flat-square)](https://packagist.org/packages/n7olkachev/laravel-computed-properties)\n[![Licence](https://img.shields.io/packagist/l/n7olkachev/laravel-computed-properties.svg?style=flat-square)](https://packagist.org/packages/n7olkachev/laravel-computed-properties)\n[![Build Status](https://travis-ci.org/n7olkachev/laravel-computed-properties.svg?branch=master)](https://travis-ci.org/n7olkachev/laravel-computed-properties)\n\n**Laravel 5.4+**\n\nBased on this tweet: https://twitter.com/reinink/status/899713609722449920\n\nSome examples for better understanding of this power:\n\n```php\nclass Order extends Model\n{\n    use ComputedProperties;\n\n    public function products()\n    {\n        return $this-\u003ehasMany(OrderProduct::class);\n    }\n\n    public function computedSum($order)\n    {\n        return OrderProduct::select(new Expression('sum(price * count)'))\n            -\u003ewhere('order_id', $order-\u003eid);\n    }\n}\n```\n\nNow, we can get order sum with `$order-\u003esum`. 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:\n\n```php\n$orders = Order::withComputed('sum')-\u003eget()\n```\n\nWe eager loaded `sum` attribute without `N+1` problem.\n\nBut there is more! You can add `having` or `orderBy` clauses to such queries for filtering and sorting!\n\n```php\nOrder::withComputed('sum')-\u003eorderBy('sum', 'desc')-\u003eget()\n```\n\n## Installation\n\nYou can install the package via composer:\n\n``` bash\ncomposer require n7olkachev/laravel-computed-properties\n```\n\nNext, add ComputedProperties trait to your models:\n\n```php\nuse ComputedProperties;\n```\n\nThat's all!\n\n## More examples\n\n```php\nclass Page extends Model\n{\n    use ComputedProperties;\n\n    public $timestamps = false;\n\n    protected $casts = [\n        'last_view' =\u003e 'datetime',\n        'first_view' =\u003e 'datetime',\n    ];\n\n    public function computedLastView($page)\n    {\n        return PageView::select(new Expression('max(viewed_at)'))\n            -\u003ewhere('page_id', $page-\u003eid);\n    }\n\n    public function computedFirstView($page)\n    {\n        return PageView::select(new Expression('min(viewed_at)'))\n            -\u003ewhere('page_id', $page-\u003eid);\n    }\n}\n```\n\nWe can find `Page` by its first view:\n\n```php\n$page = Page::withComputed('first_view')\n    -\u003ehaving('first_view', Carbon::create(2017, 8, 16, 0, 0, 0))\n    -\u003efirst();\n```\n\nOr by both `first_view` and `last_view`\n\n```php\n$page = Page::withComputed(['first_view', 'last_view'])\n    -\u003ehaving('first_view', Carbon::create(2017, 8, 16, 0, 0, 0))\n    -\u003ehaving('last_view', Carbon::create(2017, 8, 21, 0, 0, 0))\n    -\u003efirst();\n```\n\nWe can order pages by theirs `last_view`\n```php\n$pages = Page::withComputed('last_view')\n    -\u003eorderBy('last_view', 'desc')\n    -\u003eget()\n```\n\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Credits\n\n- [Jonathan Reinink](https://github.com/reinink) (idea)\n- [Nikita Tolkachev](https://github.com/n7olkachev)\n\n## Sponsored by\n\nhttps://websecret.by/\n\nWeb agency based in Minsk, Belarus\n\n## License\n\nThe MIT License (MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn7olkachev%2Flaravel-computed-properties","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn7olkachev%2Flaravel-computed-properties","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn7olkachev%2Flaravel-computed-properties/lists"}