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] Make whereBelongsTo accept Collection #41733

Merged
merged 3 commits into from
Mar 30, 2022

Conversation

erikgaal
Copy link
Contributor

@erikgaal erikgaal commented Mar 29, 2022

Hi there!

Extending upon #38927, I'm submitting this PR that allows you to retrieve records using a collection of related records.

Previously, if you wanted to retrieve, for example, all Posts that belong to one of multiple Categories, you would need to perform the following query:

$query->whereBelongsTo($category[0])
    ->orWhereBelongsTo($category[1])
    // etc...

// Or:
$query->whereIn('category_id', $categories->modelKeys());

Same as in the aforementioned PR, you would need to know the key names when building and maintain these if they change. This PR adds support for Collections passed to the whereBelongsTo method:

$query->whereBelongsTo($categories);
$query->whereBelongsTo($categories, 'category');

@erikgaal erikgaal force-pushed the egaal/belongs-to-one-of-query branch from e720511 to 605ec9e Compare March 29, 2022 08:18
@a-bashtannik
Copy link
Contributor

Could somebody explain or point to the docs, why whereIn accepts a flag boolean supposing 2 values: and and or while the SQL IN operator is a shorthand for multiple or conditions? Thanks!

@erikgaal
Copy link
Contributor Author

erikgaal commented Mar 29, 2022

@a-bashtannik It allows you to make the distinction between adding and "column" in ? and or "column" in ? to the SQL query. It's the same for all where methods in the Builder.

@a-bashtannik
Copy link
Contributor

@erikgaal argghh so simple true, thanks!

@taylorotwell
Copy link
Member

@erikgaal did you give any thoughts to just baking this functionality into whereBelongsTo?

@erikgaal
Copy link
Contributor Author

erikgaal commented Mar 29, 2022

@erikgaal did you give any thoughts to just baking this functionality into whereBelongsTo?

No, I haven't. Seems logical to support both a single Model and a Collection as arguments. If that works for you too, I'm happy to implement it that way!

@taylorotwell
Copy link
Member

@erikgaal yeah I think it's fine to bake this behavior into the existing method.

@erikgaal erikgaal changed the title [9.x] Add whereBelongsToOneOf Eloquent builder method [9.x] Make whereBelongsTo accept Collection Mar 30, 2022
@erikgaal erikgaal force-pushed the egaal/belongs-to-one-of-query branch 2 times, most recently from 4268af6 to d260892 Compare March 30, 2022 10:46
@erikgaal erikgaal force-pushed the egaal/belongs-to-one-of-query branch from d260892 to 3fef433 Compare March 30, 2022 10:50
@erikgaal
Copy link
Contributor Author

@taylorotwell Done!

@taylorotwell taylorotwell merged commit 5694e1d into laravel:9.x Mar 30, 2022
@erikgaal erikgaal deleted the egaal/belongs-to-one-of-query branch April 12, 2022 20:21
@Bosphoramus
Copy link

Hi @erikgaal would it be possible for empty collections to be passed?

Id expect it to work the same as if i pass [] to whereIn(), no records would be returned, this would be handy to avoid some conditional checks.

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.

4 participants