-
Notifications
You must be signed in to change notification settings - Fork 28
New Eloquent method whereHasAndWith #606
Comments
You are just duplicating stuff.. |
You dont understand. |
no.. you can constraint your eager loads by passing a closure to it |
... |
Just to clarify the problem that is indeed real // The first user
factory(User::class)->create()->posts()->create([
'title' => 'Foo',
]);
// The second user
factory(User::class)->create()->posts()->create([
'title' => 'Bar',
]); $constraint = function ($builder) {
$builder->where('title', 'Foo');
} With
User::with(['posts' => $constraint])->get(); [
{
"id": 1,
"posts": [
{ "id": 1, "user_id": 1, "title": "Foo" }
]
},
{
"id": 2,
"posts": []
}
] With and whereHas
User::whereHas('posts', $constraint]
->with(['posts' => $constraint])
->get(); [
{
"id": 1,
"posts": [
{ "id": 1, "user_id": 1, "title": "Foo" }
]
}
] |
My suggestion is replace this: $constraint = function ($builder) {
$builder->where('title', 'Foo');
}
User::whereHas('posts', $constraint]
->with(['posts' => $constraint])
->get(); To this: User::whereHasAndWith('posts', function ($builder) {
$builder->where('title', 'Foo');
}])->get(); |
it makes no sense to get items that are not related to the parent. |
@Dylan-DPC Dude, u totally dont understand what we want ) |
With new method |
I understood what you want from your example. but it makes no sense to have a function that returns rows that are not related to the parent model. Also why don't you query the relation from |
We need get users, who has posts only with title Foo and we need load posts to users only with title Foo. |
// Charge all users that has unpaid payments
// and has a active credit card.
// Users with inactive credit card are dealt
// with somewhere else.
User::with([
'payments' => function ($query) {
$query->where('paid', false);
},
'creditCard' => function ($query) {
$query->where('active', true);
},
])
->where('can_pay', true)
->get()
->each(function (User $user) {
$user->charge($user->payments->sum('price'));
}); With the query above you get all the users with How would you query this example via I see the proposed |
Why not write a query scope for it? |
Actually, I'm not sure why this is even necessary. Taking this example:
Surely the second use of Still, you could turn it into a query scope to make it shorter. |
@Riari That's the case! Without constraint for |
if you consider it a bug, open an issue in laravel/framework as well so that others can investigate |
Ok, thanks. |
It's not a bug. |
We'll see what people say ) |
Who votes to adding new method |
https://github.com/salomoni/whereHasAndWith Actual Laravel app that demonstrates the problem. See The fourth column is the one we are after here. Neither of in the middle as you guys very hard try to suggest. |
No harm in making suggestions that OP apparently didn't try or mention at first. Anyway, this is easily solved either by writing a query scope or tackling the query from a different angle; in the Users and Posts example, starting with a query builder for Posts makes more sense because the constraining is exclusively being done on that side:
Or if applicable, do both. The main problem I have with |
I agree the method naming could use re-thinking. The User-Post example was the simplest one I could think. A real life scenario would require couple more relations to load and more where clauses to apply. At some point you are facing the same problem what ever side you look at it. Also the data structure isn't ideal when queried through posts. And the user data is duplicated if many posts share the same user. |
Who's to say the examples given so far don't qualify as realistic? Real scenarios aren't always more complex. This doesn't change the fact that you can just write a query scope. Hell, you could use one to implement this exact feature request yourself. |
I've come across this myself. Would like to see this happen. Not sure if it can be just a method to make it easier to re-use the callback, or the actual subquery could be re-used. |
Your proposal obliges you to always use scopes. That can be not always convenient. |
In my, everything is clear. Name of method speaks for itself: |
Can you give an example? In my mind, this is a perfect example of the kind of problem query scopes are designed to resolve: when you're chaining Eloquent methods and repeating code. You can get exactly what you're asking for in this issue by making use of it.
It may be clear to you; that doesn't necessarily mean it would be to anyone else. I personally don't think it fits into Laravel conventions. |
|
Since it doesn't looks like Taylor likes this idea, you can add it to your own applications with a simple query scope (perhaps on a abstract parent class or a trait) public function scopeOnlyWith($q, $relation, $constraint)
{
$q->whereHas($relation, $constraint)
->with([$relation => $constraint]);
} |
If i need use
whereHas
and append same result throughwith
method, i have to duplicate constraint closure:New method will resolve this inconvenience:
The text was updated successfully, but these errors were encountered: