{"id":14975280,"url":"https://github.com/imanghafoori1/eloquent-relativity","last_synced_at":"2025-04-05T07:07:54.987Z","repository":{"id":34887161,"uuid":"186496125","full_name":"imanghafoori1/eloquent-relativity","owner":"imanghafoori1","description":"Allows you to decouple your eloquent models from one another.","archived":false,"fork":false,"pushed_at":"2024-04-17T12:26:22.000Z","size":84,"stargazers_count":150,"open_issues_count":1,"forks_count":14,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T06:08:27.544Z","etag":null,"topics":["eloquent","eloquent-models","laravel-package","modularity"],"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/imanghafoori1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2019-05-13T21:10:26.000Z","updated_at":"2025-02-11T21:34:35.000Z","dependencies_parsed_at":"2024-04-17T13:43:32.977Z","dependency_job_id":"6c454131-7653-4f74-96d1-c36582a2e5d2","html_url":"https://github.com/imanghafoori1/eloquent-relativity","commit_stats":{"total_commits":76,"total_committers":5,"mean_commits":15.2,"dds":0.06578947368421051,"last_synced_commit":"a80c43a9737001bb08fe6e2fff9c9bd6b12c80f8"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imanghafoori1%2Feloquent-relativity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imanghafoori1%2Feloquent-relativity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imanghafoori1%2Feloquent-relativity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imanghafoori1%2Feloquent-relativity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imanghafoori1","download_url":"https://codeload.github.com/imanghafoori1/eloquent-relativity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299833,"owners_count":20916190,"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","eloquent-models","laravel-package","modularity"],"created_at":"2024-09-24T13:51:48.931Z","updated_at":"2025-04-05T07:07:54.962Z","avatar_url":"https://github.com/imanghafoori1.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Relativity\n\nThis allows you to decouple your eloquent models from one another, by defining relations dynamically at run-time.\n\n### - Note that this package is NOT needed in laravel 7.x or above.\nRead more: \nhttps://laravel.com/docs/7.x/eloquent-relationships#dynamic-relationships\n\n   \u003cimg width=\"600px\" src=\"https://user-images.githubusercontent.com/6961695/57988296-be261180-7aa1-11e9-9e28-645ab0da75dd.png\" alt=\"widgetize_header\"\u003e\u003c/img\u003e\n\n[![Build Status](https://travis-ci.org/imanghafoori1/eloquent-relativity.svg?branch=master)](https://travis-ci.org/imanghafoori1/eloquent-relativity)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/imanghafoori1/eloquent-relativity/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/imanghafoori1/eloquent-relativity/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/imanghafoori1/eloquent-relativity/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/imanghafoori1/eloquent-relativity/?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/imanghafoori/eloquent-relativity/v/stable)](https://packagist.org/packages/imanghafoori/eloquent-relativity)\n[![StyleCI](https://github.styleci.io/repos/186496125/shield?branch=master)](https://github.styleci.io/repos/186496125)\n[![Total Downloads](https://poser.pugx.org/imanghafoori/eloquent-relativity/downloads)](https://packagist.org/packages/imanghafoori/eloquent-relativity)\n\n#### Compatibility :\n\nLaravel version 5.5 and above including version 6\n\n#### :arrow_forward: A problem which stops true modularity :\n\nLet's face it, imagine you have a modular blog application.\n\nThen, you want to add a `commenting` feature to it, so that users can comment on your articles.\n\nIn a modular structure, you have 2 modules (`user` module and `blog` module) and you will add a new module for `comments`\n\n#### :arrow_forward: let's analyze dependencies and couplings :\n\nHere the `blog` module \"knows\" and \"depends\" upon the `user` module.\n\nBut the `user` module should not know or care about the `blog` module. The `blog` is a `plug-in` on the top of the `user` module.\n\nNow we want to add a `comment` module, on the top of `user` and `blog` module.\n\n\n\u003cimg width=\"600px\" src=\"https://user-images.githubusercontent.com/6961695/57987611-26242a00-7a99-11e9-8c32-67e76b57420f.jpg\"\u003e\u003c/img\u003e\n\n\n#### :arrow_forward: The Right way :\n\nIn a truely modular system when you add the `comments`, you should NOT go and touch the code within the `users` or `blog` module.\n(Remember the `open-closed` principle in `SOLID` ?!)\n\nImagine you are in a team and each member is working on a seperate module.\n\n`Blog` module is not yours. your team mate is responsible for it and is allowed to code on it.\n\nBut when you want to start to define the eloquent relations between `Comment` and `User` and `Article` models, you immediately realize that you have to put code on the eloquent models of other modules to define the inverse of the relationships. Crap ! \n\n\nLook How everything is pointing inward.\n\nIf you look at the `User` folder you will have absolutely no footprint of Comment or Article.\n\nWe have to touch the code of both `Blog` and `User` module when add a new `comment` module.\n\nFor example : You have to open `User.php` and define the\n```php\npublic function comments() {\n    return $this-\u003ehasMany(Comment::class); \n}\n```\nand this is a no no, because it makes an arrow from inside to outside.\n\nSo what to do ?!\n\nHow can `Comment` be introduced to the system without modifying the other modules ?! (@_@)\n\n\n#### :arrow_forward: Install: (the most painful step)\n\n```\ncomposer require imanghafoori/eloquent-relativity   (and take a coffee...)\n```\n\nNow the installtion finished, you first have to make your models \"relative\" !!!\n\nBy using the `Imanghafoori\\Relativity\\DynamicRelations` traits on your eloquent models.\n\n![image](https://user-images.githubusercontent.com/6961695/58089939-465c0200-7bdb-11e9-8df0-2dc5212ced43.png)\n\n\nSo the `User`, `Article`, `Comment` will have to have this trait one them.\n\nNow comes the sweet part :\n\nwithin the `CommentsServiceProvider.php`\n\n```php\nclass CommentsServiceProvider \n{\n    public function register () {\n        \n        User::has_many('comments', Comment::class);     // instead of defining method on the User class.\n        Article::has_many('comments',  Comment::class);\n        \n        Comment::belongs_to('author', User::class);       // inverse of relations\n        Comment::belongs_to('article',  Article::class);\n    }\n\n}\n\n```\n\n\nNow you can do these queries :\n\n```php\nUser::find(1)-\u003ecomments;\nor \nUser::find(1)-\u003ecomments()-\u003ecount();\n```\n\nSo instead of going to `User` model and define a method there...\n```php\npublic function comments() {\n    return $this-\u003ehasMany(Comment::class); \n}\n```\n\nYou have defined the method remotely from your new module at run-time: \n\n ```php\n User::has_many('comments', Comment::class);\n ```\n \n Here is a list of supported relations :\n \n- has_many\n- has_one\n- belongs_to\n- belongs_to_many\n- morph_to_many\n- morph_many\n- morph_one\n- morph_to\n- morphed_by_many\n- has_many_through\n \nThey accept the same paramters as the eloquent equivalent counter part. except the first argument should be relation name.\n\n#### :arrow_forward: Extra features :\n\n\nsometimes you need to call extra methods on the relations.\n\n```php\nUser::has_many('comments', Comment::class)-\u003eorderBy('id', 'asc');\n```\n\nAll the methods are available to you.\n\n- Enforce eager-loading\n\nOn reqular eloquent models you may define the\n\n```php\nUser extends Model {\n    protected $with = ['comments'];\n}\n```\n\ninstead you can :\n\n```php\n\nUser::forceEagerLoading('comments');\n\n```\n\nremember this should be in the `boot` method of your Service Provider not the `register` method.\n\n\n### :star: Your Stars Make Us Do More :star:\nAs always if you found this package useful and you want to encourage us to maintain and work on it, Please press the star button to declare your willing.\n\n\n### :arrow_forward: More from the author:\n\n### Laravel Terminator\n\n :gem: A minimal yet powerful package to give you opportunity to refactor your controllers.\n\n- https://github.com/imanghafoori1/laravel-terminator\n\n------------------\n\n### Laravel Widgetize\n\n :gem: A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.\n\n- https://github.com/imanghafoori1/laravel-widgetize\n\n\n-------------------\n\n### Laravel Master Pass\n\n :gem: A simple package that lets you easily impersonate your users.\n\n- https://github.com/imanghafoori1/laravel-MasterPass\n\n------------------\n\n### Laravel HeyMan\n\n :gem: It allows to write exressive and defensive code which is decoupled from the rest of your app.\n\n- https://github.com/imanghafoori1/laravel-heyman\n\n----------------\n\n### 🍌 Reward me a banana 🍌\n\nso that I will have energy to start the next package for you.\n\n\nDodge Coin: DJEZr6GJ4Vx37LGF3zSng711AFZzmJTouN\n\nLiteCoin: ltc1q82gnjkend684c5hvprg95fnja0ktjdfrhcu4c4\n\nBitCoin: bc1q53dys3jkv0h4vhl88yqhqzyujvk35x8wad7uf9\n\nRipple: rJwrb2v1TR6rAHRWwcYvNZxjDN2bYpYXhZ\n\nEtherium: 0xa4898246820bbC8f677A97C2B73e6DBB9510151e\n\n----------------\n\n\u003cp align=\"center\"\u003e\n  \n    Life is like riding a bicycle. To keep your balance you must keep moving.\n    \n    \"Albert Einstein\"\n    \n\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimanghafoori1%2Feloquent-relativity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimanghafoori1%2Feloquent-relativity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimanghafoori1%2Feloquent-relativity/lists"}