{"id":28515953,"url":"https://github.com/thybag/bonus-laravel-relations","last_synced_at":"2025-07-05T17:31:06.303Z","repository":{"id":32669009,"uuid":"139284475","full_name":"thybag/bonus-laravel-relations","owner":"thybag","description":"A selection of weird \u0026 wonderful additional relation types for laravel's eloquent ORM","archived":false,"fork":false,"pushed_at":"2025-05-06T01:49:19.000Z","size":244,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-09T03:37:29.748Z","etag":null,"topics":["eloquent","laravel"],"latest_commit_sha":null,"homepage":null,"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/thybag.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","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":"2018-06-30T22:27:37.000Z","updated_at":"2025-04-02T15:50:22.000Z","dependencies_parsed_at":"2024-07-18T19:10:56.975Z","dependency_job_id":null,"html_url":"https://github.com/thybag/bonus-laravel-relations","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/thybag/bonus-laravel-relations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thybag%2Fbonus-laravel-relations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thybag%2Fbonus-laravel-relations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thybag%2Fbonus-laravel-relations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thybag%2Fbonus-laravel-relations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thybag","download_url":"https://codeload.github.com/thybag/bonus-laravel-relations/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thybag%2Fbonus-laravel-relations/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263778354,"owners_count":23509980,"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":"2025-06-09T03:31:25.875Z","updated_at":"2025-07-05T17:31:06.292Z","avatar_url":"https://github.com/thybag.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Bonus Eloquent relations for Laravel\n\nA selection of weird \u0026 wonderful additional relation types for laravel's eloquent ORM I've ended up needing.\n\nMany of these are experimental and may behave in unexpected \u0026 none standard ways.\n\nOn the bright side - it's tested!       \n![Build Status](https://github.com/thybag/bonus-laravel-relations/actions/workflows/test.yml/badge.svg)\n \nAll licensed under the MIT license.\n\n## Usage.\n\n1. Install via composer using `composer require thybag/bonus-laravel-relations`\n2. Include the `use thybag\\BonusLaravelRelations\\Traits\\BonusRelationsTrait;` in to your model (or base model if you want them everywhere.)\n3. Use the relations as you would any other.\n\nThe relation traits can also be added individually if you prefer that.\n\n* Run tests with `composer test`\n* Run lint with `composer lint`\n\n# Relations \n\n### BelongsToMorph\nGet polymorphic relations of a single type.\n\n```php\npublic function shop()\n{\n    return $this-\u003ebelongsToMorph(Shop::class, 'noteable');\n}\n```\n\n### HasManyViaMany\nDefine a relation via an unlimited number of middle tables.\n\n```php\npublic function products()\n{\n    return $this-\u003ehasManyViaMany(Product::class)-\u003evia(Shop::class)-\u003evia(Franchise::class);\n}\n```\n\n### HasAggregate\nGet an aggregate result as a relation. The main benefit of this approach is it allows easy lazy loading of this data on collections + interaction with the results in a more eloquent like way.\n\n```php\npublic function productTotals()\n{\n    return $this-\u003ehasAggregate(Product::class)-\u003eselectRaw('\n        COUNT(DISTINCT products.id) AS unique_products,\n        SUM(products.amount) * AVG(products.value) AS stock_value,\n        SUM(products.amount) AS total_products,\n        AVG(products.value) AS average_product_value\n    ');\n}\n```\n\n### HasMethod\nUse a local method as if it were a relation.\n\n```php\npublic function totalValue()\n{\n    return $this-\u003ehasMethod(function () {\n        return ['total' =\u003e ($this-\u003eamount * $this-\u003evalue)];\n    });\n}\n```\n\n### BelongsToOne\nDefine a one-to-one relation through a pivot table.\n\n```php\npublic function latestRating()\n{\n    return $this-\u003ebelongsToOne(Rating::class, 'shop_rating')-\u003elatest('created_at');\n}\n```\n\n# InertModel\n\nAs the relationships `HasMethod` and `HasAggregate` don't return a traditional model from the database, a special model called type `InertModel` is used. This allows the relationship to fill the model with arbitrary attributes without the risk of causing unexpected behavior.\n\nIf you would like to use a custom InertModel rather than the one provided, create a config called `bonus-laravel-relationships.php` in your config folder, then set the value `inertModel` with the class path to the model you would like to use instead. I would recommend whatever model you use inherits from the base IntertModel to avoid unexpected behavior.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthybag%2Fbonus-laravel-relations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthybag%2Fbonus-laravel-relations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthybag%2Fbonus-laravel-relations/lists"}