-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[5.5] Belongs to many related local key #17915
[5.5] Belongs to many related local key #17915
Conversation
In this really needed. It's starting to feel silly. Passing 6-7 arguments to belongs to many? |
There are a lot of needed and potentially useful options. It's a compromise, most of them are optional arguments. Why HasOne, HasMany and BelongsTo have all the available options out-of-the-box and BelongsToMany not? Working with legacy dbs, I had a lot of issue about this topic. What do you think? |
One suggestion would be to keep the belongsToMany method in the HasRelationships trait the same and use setters for both keys instead. The setters can return '$this'. This way the parameter count remains the same, but one can adjust the relation for that one particular case. |
It would be nice to have a fluent interface for setting up relationships. return $this->belongsToMany('App/Role', function ($relationship) {
$relationship->setTable('role_user')
->setForeignKey('user_email')
->setRelatedKey('role_code')
->setParentKey('email')
->setLocalKey('code')
->setRelationName('foo');
}); This way you wouldn't have to remember the order of arguments and also if you just want to change eg. the relation name you wouldn't have to pass the silly |
There is at least one problem with implementing this solution.
We could always bind the closure to the relationship object(and use protected methods) but this would be probably even more confusing to use One way to solve this is to introduce an intermediate object accepting all needed parameters and then using it to copy the values to the relationship object in the constructor. Or, you know, the |
@KKSzymanowski nice idea. |
@KKSzymanowski I was definitely going to have a go with this idea myself. 😄 Makes much more sense to me, I do sometimes forget the order of the foreign key and local key. With your example it makes it very obvious what all the columns are. Not sure on the closure business, but I'm think this could be adapted to allow both ways. I would lose the $this->belongsToMany(Role::class)->foreignKey(...)->relatedKey(...) 👍 |
The idea is cool, more fluent and clear (clearer than the list of parameters). @arjasco your example collides with the confusion issue raised by @KKSzymanowski: the new methods you suggest, will be available also after the relationship definition. |
@taylorotwell Are you interested in my proposal? I can have it done by the weekend for all relationships. |
Things get complicated sometimes, might not be useful for everyday but optional configuration is a must thing. |
@KKSzymanowski i think it looks pretty good. I would avoid "set" on the front of all the methods and just have it be like ->table ->foreignKey, etc. |
@taylorotwell Do you want it for 5.4 or 5.5? |
Hi, I've added also the configurability of the "related" local key using BelongsToMany relationship.
Take this example
With this PR we can define this relationship like this:
PS sorry for this second PR, I should have done one.