-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Allow for converting a HasMany to HasOne && MorphMany to MorphOne #46443
[10.x] Allow for converting a HasMany to HasOne && MorphMany to MorphOne #46443
Conversation
d46f0a2
to
91283d8
Compare
If this is something that makes sense, will gladly add HasManyThrough@hasOneThrough() method as well |
91283d8
to
1a871f8
Compare
Thanks |
@cosmastech could you please check which sql query will be generated? I saw that the sql query has duplicated conditions... |
@siarheipashkevich could you provide a code sample the generated SQL? edit: I see now. $user->userPreferences()->one()->toSql();
// select * from `user_preferences` where `user_preferences`.`user_id` = ? and `user_preferences`.`user_id` is not null and `user_preferences`.`user_id` = ? and `user_preferences`.`user_id` is not null |
Please take a look at your sql. You also have diplicated conditions
|
@siarheipashkevich thank you for reporting this! #46575 should resolve this issue |
Problem
There are often times where I am building a HasMany relationship and I need to get just one item from the collection. This means I have to either define the relationship two times (one for HasMany and then one for HasOne->ofMany()).
As you can imagine, if the design of the DB changes (like during early development of a project) or if the relationship isn't simple, this can be cumbersome or lead to duplication of work.
Proposed Solution
What is added in this PR is the ability to convert a HasMany relationship to HasOne (and MorphMany to MorphOne). So the above code can become