{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/internals/issues/668","repository_url":"https://api.github.com/repos/laravel/internals","labels_url":"https://api.github.com/repos/laravel/internals/issues/668/labels{/name}","comments_url":"https://api.github.com/repos/laravel/internals/issues/668/comments","events_url":"https://api.github.com/repos/laravel/internals/issues/668/events","html_url":"https://github.com/laravel/internals/issues/668","id":240123501,"number":668,"title":"joinRelation","user":{"login":"decadence","id":1434668,"avatar_url":"https://avatars0.githubusercontent.com/u/1434668?v=4","gravatar_id":"","url":"https://api.github.com/users/decadence","html_url":"https://github.com/decadence","followers_url":"https://api.github.com/users/decadence/followers","following_url":"https://api.github.com/users/decadence/following{/other_user}","gists_url":"https://api.github.com/users/decadence/gists{/gist_id}","starred_url":"https://api.github.com/users/decadence/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/decadence/subscriptions","organizations_url":"https://api.github.com/users/decadence/orgs","repos_url":"https://api.github.com/users/decadence/repos","events_url":"https://api.github.com/users/decadence/events{/privacy}","received_events_url":"https://api.github.com/users/decadence/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":16,"created_at":"2017-07-03T09:57:36Z","updated_at":"2018-02-16T18:27:03Z","closed_at":null,"author_association":"NONE","body":"As we know Eloquent uses separate query for relationships but sometimes we need JOIN relationship (for sorting by relationship column or for aggregate functions like `sum`, `count` etc.)\r\nThen we have to use `join` method on `Builder` and we need to specify joining columns with hardcoded names:\r\n`User::join(\"posts\", \"user.id\", \"=\", \"posts.user_id\")`\r\n\r\nIt would be cool to have method `joinRelation` which could determine needed parameters automatically from relation definition:.\r\n\r\nFor example:\r\n```php\r\npublic function posts()\r\n{\r\n   return $this->hasMany(Posts::class, \"user_id\");\r\n}\r\n```\r\n\r\nand then (just an example):\r\n```php\r\nUser::joinRelation(\"posts\")->selectRaw(\"SUM('posts.views') as total_views\")\r\n```\r\nwithout need for check column names.\r\n\r\nJust ping @browner12 and @sileence in case they are interested in this and could implement this."},"comment":{"url":"https://api.github.com/repos/laravel/internals/issues/comments/366318724","html_url":"https://github.com/laravel/internals/issues/668#issuecomment-366318724","issue_url":"https://api.github.com/repos/laravel/internals/issues/668","id":366318724,"user":{"login":"MatteoOreficeIT","id":8988799,"avatar_url":"https://avatars2.githubusercontent.com/u/8988799?v=4","gravatar_id":"","url":"https://api.github.com/users/MatteoOreficeIT","html_url":"https://github.com/MatteoOreficeIT","followers_url":"https://api.github.com/users/MatteoOreficeIT/followers","following_url":"https://api.github.com/users/MatteoOreficeIT/following{/other_user}","gists_url":"https://api.github.com/users/MatteoOreficeIT/gists{/gist_id}","starred_url":"https://api.github.com/users/MatteoOreficeIT/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MatteoOreficeIT/subscriptions","organizations_url":"https://api.github.com/users/MatteoOreficeIT/orgs","repos_url":"https://api.github.com/users/MatteoOreficeIT/repos","events_url":"https://api.github.com/users/MatteoOreficeIT/events{/privacy}","received_events_url":"https://api.github.com/users/MatteoOreficeIT/received_events","type":"User","site_admin":false},"created_at":"2018-02-16T18:27:03Z","updated_at":"2018-02-16T18:27:03Z","author_association":"NONE","body":"Hi Guys \r\n\r\nI wrote a gist with an useful Trait about query join through relations \r\n\r\nhttps://gist.github.com/MatteoOreficeIT/d3f66e90436dd5b9c90fbe144118e667\r\n\r\nExample \r\n\r\n```php\r\npublic function scopeJoinWith(Builder $builder,$relationSegments,$rightAlias=null,$operator='=',$join='join')\r\n// some usage examples\r\n\\App\\Terminali::query()->select('S.*')->joinWith('settori','S');\r\n\\App\\Terminali::query()->select('P.*')->joinWith(['settori','prodotto'],'P');\r\n\\App\\Terminali::query()->select('P.*','S.*')->joinWith(['settori','prodotto'],['P','S']);\r\n\\App\\Terminali::query()->from(\\App\\User::query()->getModel()->getTable().' as U')->select('U.*','S.*')->joinWith(['settori','prodotto'],['P','S']);\r\n```\r\n\r\nIt works on BelongsTo and HasOneOrMany \r\n\r\nI hope it could be integrated in the future\r\n"}}
{"actor":{"display_login":"MatteoOreficeIT"}}