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

[12.x] Changed whereRelation to work with only two params when the $column variable is not instance of \Closure #54959

Closed
wants to merge 1 commit into from

Conversation

habibendris
Copy link
Contributor

@habibendris habibendris commented Mar 10, 2025

The Eloquent whereRelation accepts 4 parameters in total $relation, $column, $operator = null, $value = null

So whenever we want to query we have to use the whereRelation as follows.

$posts = Post::query()
    ->whereRelation('comments', 'user_id', Auth::id())
    ->get()

We always have to use atleast 3 parameters if the $column is not instance of \Closure.
This pull request allows us to only use 2 parameters for this usecase.

$posts = Post::query()
    ->whereRelation('comments.user_id', Auth::id())
    ->get()

@shaedrich
Copy link
Contributor

The title of your PR is a little misleading as whereRelation() does indeed work with just two params since the latter two are both optional. It just doesn't for your use case, but it does for when you want to check if the value is null. So I would rename your PR to something like "Add dot notation support to whereRelation".

@habibendris habibendris changed the title [12.x] Changed whereRelation to work with only two params [12.x] Changed whereRelation to work with only two params when the $column variable is not instance of \Closure Mar 10, 2025
@habibendris habibendris changed the title [12.x] Changed whereRelation to work with only two params when the $column variable is not instance of \Closure [12.x] Changed whereRelation to work with only two params when the $column variable is not instance of \Closure Mar 10, 2025
@macropay-solutions
Copy link

macropay-solutions commented Mar 10, 2025

To avoid breaking change, the column variable can stay the same and just the dockblock would need changing.

…$column variable is not instance of \Closure
@habibendris
Copy link
Contributor Author

To avoid breaking change, the column variable can stay the same and just the dockblock would need changing.

I have updated the commit as needed

@@ -428,6 +428,14 @@ public function orWhereDoesntHaveMorph($relation, $types, ?Closure $callback = n
*/
public function whereRelation($relation, $column, $operator = null, $value = null)
{
if (! ($column instanceof Closure) && $operator == null && $value == null) {
$relationKeys = explode('.', $relation);

Copy link
Contributor

@mohammadrasoulasghari mohammadrasoulasghari Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the StyleCI issue by removing additional spaces in the line. 🧹
issue link

Let all the checks pass.👌

@taylorotwell
Copy link
Member

I am closing this pull request because it lacks sufficient explanation, tests, or both. It is difficult for us to merge pull requests without these things because the change may introduce breaking changes to the framework.

Feel free to re-submit your change with a thorough explanation of the feature and tests - integration tests are preferred over unit tests. Please include it's benefit to end users; the reasons it does not break any existing features; how it makes building web applications easier, etc.

Thanks!

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.

5 participants