Skip to content

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

Merged
taylorotwell merged 4 commits into
laravel:9.xfrom
adideas:9.x
May 24, 2022
Merged

[9.x] Model::whereRelation add callback function#42491
taylorotwell merged 4 commits into
laravel:9.xfrom
adideas:9.x

Conversation

@adideas

@adideas adideas commented May 24, 2022

Copy link
Copy Markdown
Contributor

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

adideas commented May 24, 2022

Copy link
Copy Markdown
Contributor Author

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