Proposal: pass arguments to relationship functions #45092
Unanswered
valentinomariotto
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I could really use the ability to set an argument to a relationship function from a method such as whereHas, has, doesntHave() etc
I'm not talking about the query callback function. I'm talking about the relationship function itself.
Consider a model like so:
And a related model featuring the following relationships:
As you can see my relationships
progenial_relation
andparental_relation
accept an argument (the date).Now, if you wanted to use those relationships straightforwardly like so, there's no issues:
But what happens if you need to use eager-loading methods such as
has()
,whereHas()
,doesntHave()
,whereDoesntHave()
?How do you pass an argument to the relationship? For example, I wanted to add other relationships to my User model.
For completeness I'm going to add what happens if I use a normal closure:
This is the resulting SQL. As you can see the constraints are applied twice. Once by the query callback and once by the relationship. But since I cannot pass the correct argument to the relationship, it gets the default one. The 2 constraints collide and the query does not work.
I think it would be great if we could pass an argument to the relationship directly, with a syntax such as this:
whereHas('relationship_name', $callback, [$arg1, $arg2]);
Thank you
PS: I also asked on the forums but probably it wasn't the right place https://laravel.io/forum/feature-request-pass-arguments-to-relationship-functions/edit
Beta Was this translation helpful? Give feedback.
All reactions