-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Add closure-based relationship setup #18313
Conversation
Your Setter class probably isn't needed. You could probably just use Illuminate\Support\Fluent. |
@taylorotwell I thought about that, but with Fluent I cannot enforce correct parameter names. If user makes a typo he won't be notified of it right away and receive unexpected behaviour sometime later. Do you think this kind of enforcement isn't necessary? |
Yeah you could perhaps extend Fluent or something. |
@taylorotwell How does it look like now? |
Why aren't the setters directly on the relationship class? Why do we need the closure? $this->belongsToMany(Foo::class)->foreignKey('...')->localKey('...'); |
@JosephSilber Because for example in That's at least how I understand it, correct me if I'm wrong. |
That is correct. I think being able to build relationship queries lazily would be useful. I've had cases where I wanted to slightly modify a relationship query but can't because something has already been added that I want to remove. Changing that might be non-trivial though due to the way |
I made some changes to belongs to many and morph to many. Can you update for that? #18380 |
Sure, I'll have it done by tomorrow morning UTC, because I don't have access to my PC right now. |
@taylorotwell Travis builds are failing because of some dependency incompatibilities. It's not my fault, is it? |
I kind of agree with Joseph that we may want to eventually pursue just chaining these methods onto the relationship itself somehow. I think that is the ideal end goal. |
This allows for explicit and elegant setup of relationships:
Instead of
you can say:
This is most useful if you need to overwrite one default value:
Instead of
you can say:
This functionality is available for all relationships:
The convention is, the first optional parameter of a method(from the list above) can be a Closure and if it is, the values set up in it are used as if they were original function arguments. If the first optional argument is a Closure, all succeeding are ignored. If, in a Closure, a value is not specified, it is treated as
null
for further processing which is equivalent to not providing the argument in the old relationship setup process.