-
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
Conditional (when helper) gives back Eloquent Builder instance inside the closure instead of the relationship instance #53292
Comments
As a workaround, the Eloquent Builder returned is the one being built by the So you can use this call in the meanwhile: return MyModel::find(2)
->someRelationship()
// replace "pivot" by the pivot table's name,
// in case you are using a custom name
->when(true, fn ($query) => $query->whereBetween('pivot.updated_at', ['2000-05-05', '2024-05-05']))
->get(); You can try sending a PR, adding the |
@rodrigopedra then that trait will be included in relation twice. Once in the relation and once in the Eloquent Builder... |
@marius-mcp how come? The And the Adding the trait to |
@rodrigopedra you are right. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
While this may seem like a bug, developers may already use the method and the current behavior. Please send it to |
@rodrigopedra I think also https://github.com/laravel/framework/blob/11.x/src/Illuminate/Database/Query/Builder.php the query builder should have that trait. So, Relation, Eloquent Builder and Query Builder because of the forwards call to. The reason is that on callbacks the instance of the query can be Relation, Eloquent Builder and Query Builder. |
I have sent the PR to |
@Sajid-al-islam why didn't you put the trait also on the Query Builder? |
I forgot, I am adding it |
Laravel Version
11.28.1
PHP Version
8.3
Database Driver & Version
No response
Description
Inside the when closure you will not get a relationship instance, so any specific relationship function will not be available.
Expected:
$query to be BelongsToMany instance
Actual:
$query is a Illuminate\Database\Eloquent\Builder instance
Ex. BelongsToMany relationships, here you will not be able to call the wherePivot*-methods.
Steps To Reproduce
Create a model any kind of relationship and and fire a query that uses the relationship. Like in the example above.
The text was updated successfully, but these errors were encountered: