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] Adds a simple where helper for querying relations #38499

Merged
merged 3 commits into from
Aug 26, 2021
Merged

[8.x] Adds a simple where helper for querying relations #38499

merged 3 commits into from
Aug 26, 2021

Conversation

DarkGhostHunter
Copy link
Contributor

@DarkGhostHunter DarkGhostHunter commented Aug 23, 2021

What?

This is sugar for whereHas() for simple where clauses.

// Before:
User::whereHas('posts', function ($query) {
    $query->where('published_at', '>', now());
})->get();

// After
User::whereRelation('posts', 'published_at', '>', now())->get();

How?

Basically creates the whereRelation() and orWhereRelation() helpers, and whereMorphRelation() and orWhereMorphRelation() for morph relations. Since these use the where() method underneath, you can do advanced things:

Comment::whereMorphRelation('commentable', '*', [
    'is_public' => true,
    'is_vip' => false,
])->get();

Why?

Removes code complexity by making the query more readable when dealing with simple where clauses over relations.

Breaking Changes

None - it's merely and additive PR.

@deleugpn
Copy link
Contributor

I think this one can count as a "fixed papercut" and Taylor can take the day off 😄

@DarkGhostHunter
Copy link
Contributor Author

I think this one can count as a "fixed papercut" and Taylor can take the day off 😄

Sorry I'm too retarded to understand.

@rodrigopedra
Copy link
Contributor

@DarkGhostHunter he is making a reference to this tweet from Taylor:

Laracon is September 1st. I’m trying to make a developer experience improvement to Laravel every single day leading up to the conference. What do I improve today?

link to tweet: https://twitter.com/taylorotwell/status/1427286854584393740

And, since then Taylor is making a PR each day to fix small annoyances for developers (aka papercut) suggested in that thread.

So, I think he means your PR is such a good improvement that Taylor can take a day off because of it.

@DarkGhostHunter
Copy link
Contributor Author

DarkGhostHunter commented Aug 24, 2021

@DarkGhostHunter he is making a reference to this tweet from Taylor:

Laracon is September 1st. I’m trying to make a developer experience improvement to Laravel every single day leading up to the conference. What do I improve today?

link to tweet: https://twitter.com/taylorotwell/status/1427286854584393740

And, since then Taylor is making a PR each day to fix small annoyances for developers (aka papercut) suggested in that thread.

So, I think he means your PR is such a good improvement that Taylor can take a day off because of it.

Thanks, I feel less dumber now.

@DarkGhostHunter
Copy link
Contributor Author

DarkGhostHunter commented Aug 24, 2021

I purposely left out whereRelationHas() (and its variants). This was basically syntax sugar for a nested whereHas() and whereKey().

For example, I use this to get all the Post that use one single image.

$image = Image::find(3)

// Before
$posts = Post::whereHas('images', function ($query) use ($image) {
    $query->whereKey($image);
});

// After
$posts = Post::whereRelationHas('images', $image)->get();

I would prefer whereRelationContains() nonetheless, but feels too long.

@taylorotwell taylorotwell merged commit f8e5b00 into laravel:8.x Aug 26, 2021
@Geovanek
Copy link

Geovanek commented Sep 2, 2021

Amazing.

Now all that's left is to put it in the documentation.

@driesvints
Copy link
Member

@Geovanek feel free to send in a PR 👍

@aneeskhan47
Copy link
Contributor

aneeskhan47 commented Sep 3, 2021

@DarkGhostHunter can you also do a helper for withCount withSum etc, so i don't have to write query closures 😄

@binaryk
Copy link
Contributor

binaryk commented Sep 8, 2021

Nice sugar here, but is it backward compatible? What if you have a column called relation in your User model?

The User::whereRelation('posts') was translated into where users.relation = 'posts' will this still be working?

@DarkGhostHunter
Copy link
Contributor Author

DarkGhostHunter commented Sep 8, 2021 via email

@YaakovR
Copy link

YaakovR commented Oct 10, 2021

@DarkGhostHunter Seems like whereRelation can't be passed "BETWEEN" as an operator. There also is currently is no whereRelationBetween function. Perhaps it can be added?

victorvilella pushed a commit to cdsistemas/framework that referenced this pull request Oct 12, 2021
)

* Adds simple `where` helper for relations.

* Reordered function values and type hint.

* Added helpers for morph relations.
@robsontenorio
Copy link

robsontenorio commented Dec 21, 2021

@DarkGhostHunter is there any way to make it work with IN operator?

 return $query->whereHas('posts', function (Builder $query) use ($status) {
            $query->whereIn('status', $status);
        });

@DarkGhostHunter
Copy link
Contributor Author

NOPE.

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.

None yet

10 participants