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

[9.x] Model::whereRelation add callback function #42491

Merged
merged 4 commits into from
May 24, 2022
Merged

[9.x] Model::whereRelation add callback function #42491

merged 4 commits into from
May 24, 2022

Conversation

adideas
Copy link
Contributor

@adideas adideas commented May 24, 2022

Example

// I want to optimize the query. In callback function.
$userSQL = User::whereRelation(
    "posts",
    function($query) {
        $query->limit(1)->where("public", true);
    }
)->toSql();

Before this PR

select * 
from "users" 
where exists (
   select *
   from "posts" 
   where "users"."id" = "posts"."user_id" and ("public" = ?)
)

After this PR

select * 
from "users" 
where exists (
   select *
   from "posts" 
   where "users"."id" = "posts"."user_id" and "public" = ? limit 1
)

I added tests for checks.
This is not in the documentation, but it is logical.
It would be very convenient.

I think we're talking about a supplement. I'm not sure if this is a mistake. But many expect a different result using this method.

@adideas
Copy link
Contributor Author

adideas commented May 24, 2022

I can change my approach.
Please write your idea, I'll change it.

I followed the Laravel approach.
This is not a critical addition.

@taylorotwell taylorotwell merged commit 5299c22 into laravel:9.x May 24, 2022
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.

2 participants