Skip to content
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

[8.x] Add Conditionable Trait to Relation #37640

Merged
merged 1 commit into from
Jun 11, 2021

Conversation

nedwors
Copy link
Contributor

@nedwors nedwors commented Jun 9, 2021

Hi!

Thank you for all the amazing work 🔥

Overview

This PR adds the new Conditionable trait to the base Relation class.

Currently, the following would not work for a relationship query...

$user->posts()->when($shouldOrder, fn ($query) => $query->orderByPivot('..'))->get()

...because the Relation class does not have a native when/unless. So, it forwards it to the Builder but then this does not have access to the Relation's methods - in this case orderByPivot.

Compatibility

The only thought is that this could be a breaking change in the sense that some relations override some Relation methods - eg. get in BelongsToMany. So if any apps do something along the lines of...

$user->posts()->when($shouldPaginate, fn ($query) => $query->paginate(), fn ($query) => $query->get())

...the get method would now be elevated from the Builder up to the Relation?

Tests

Testing wise, I appreciate there is only one test. I felt it illustrated the feature nicely using an existing set up. But very happy to add more if desired.

@nedwors nedwors changed the title [8.x] - Adds Conditionable Trait to Relation [8.x] Add Conditionable Trait to Relation Jun 9, 2021
@driesvints driesvints closed this Jun 9, 2021
@driesvints driesvints reopened this Jun 9, 2021
@taylorotwell taylorotwell merged commit e3adb1c into laravel:8.x Jun 11, 2021
@nedwors nedwors deleted the conditionable-relations branch June 11, 2021 16:20
taylorotwell added a commit that referenced this pull request Jun 14, 2021
taylorotwell added a commit that referenced this pull request Jun 14, 2021
@taylorotwell
Copy link
Member

Reverted due to breaking tests on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants