Replies: 2 comments
-
Hi @jmrecodes, Your |
Beta Was this translation helpful? Give feedback.
0 replies
-
@staudenmeir We were about to ask if this is a new feature of Laravel 11, passing conditions as params that are not declared in the relation's definition. $queryWithClosure = $invoice->lineItems(function ($query) {
$query->whereHas('product', function ($productQuery) {
$productQuery->where('is_membership_product', true);
});
});
public function lineItems()
{
return $this->hasMany(InvoiceProductRelationship::class)->whereHas('product', function ($query) {
$query->whereNull('deleted_at');
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
Laravel Version
11.4
PHP Version
8.3
Database Driver & Version
No response
Description
The issue is that when using a closure with the
whereHas
method on a relationship that already contains awhereHas
method, the additional condition is not being included in the generated SQL query:{
"sql_without_closure": "select * from "invoice_product_relationships" where "invoice_product_relationships"."invoice_id" = ? and "invoice_product_relationships"."invoice_id" is not null and exists (select * from "products" where "invoice_product_relationships"."product_id" = "products"."id" and "deleted_at" is null and "products"."deleted_at" is null) and exists (select * from "products" where "invoice_product_relationships"."product_id" = "products"."id" and "is_membership_product" = ? and "products"."deleted_at" is null)",
"bindings_without_closure": [2, true],
"sql_with_closure": "select * from "invoice_product_relationships" where "invoice_product_relationships"."invoice_id" = ? and "invoice_product_relationships"."invoice_id" is not null and exists (select * from "products" where "invoice_product_relationships"."product_id" = "products"."id" and "deleted_at" is null and "products"."deleted_at" is null)",
"bindings_with_closure": [2]
}
Notes:
The sql_without_closure includes the 'is_membership_product' condition as expected.
bindings_without_closure has two bindings: one for invoice_id and one for is_membership_product.
BUG: The sql_with_closure does NOT include the 'is_membership_product' condition.
BUG: bindings_with_closure only includes one binding (for invoice_id), missing the is_membership_product binding.
Steps To Reproduce
The demo's repo can be found at https://github.com/jmrecodes/laravel-whereHas-bug-demo
Beta Was this translation helpful? Give feedback.
All reactions