Skip to content

[12.x] Add whereAttachedTo() Eloquent builder method #55245

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

Merged
merged 2 commits into from
Apr 2, 2025

Conversation

bakerkretzmar
Copy link
Contributor

This PR adds a whereAttachedTo() Eloquent query builder method to simplify retrieving records that are 'attached' to a given model instance/collection with a belongsToMany relationship.

It functions very similarly to the whereBelongsTo() method introduced in #38927, and eliminates the need to pass a closure to whereHas and/or extract the parent model ID(s) manually.

Given some models:

$tags = Tag::where('created_at', '>', now()->subMonth())->get();

Before

$taggedPosts = Post::whereHas('tags', function ($query) use ($tags) {
    $query->whereKey($tags);
})->get();

After

$taggedPosts = Post::whereAttachedTo($tags)->get();

Like whereBelongsTo(), whereAttachedTo() guesses the relationship name based on Laravel conventions, but it can also be passed in manually:

$taggedPosts = Post::whereAttachedTo($tags, 'tags')->get();

Couple last things:

  • I know in the specific example above the conditions could be written directly into a has query, but that isn't always practical if they change based on other variables or if the parent model/collection is used in other places.
  • This method includes results attached to any of the models passed into it, which feels like a more common use case to me, but this could also be called whereAttachedToAny() to be more specific and leave room for a later whereAttachedToAll().

Thanks!

@bakerkretzmar bakerkretzmar changed the title [12.x] Add whereAttachedTo Eloquent builder method [12.x] Add whereAttachedTo() Eloquent builder method Apr 1, 2025
@taylorotwell taylorotwell merged commit 6a08dbd into laravel:12.x Apr 2, 2025
41 checks passed
@taylorotwell
Copy link
Member

Thanks!

@bakerkretzmar bakerkretzmar deleted the where-attached-to branch April 2, 2025 18:31
@robsontenorio
Copy link

@bakerkretzmar on your example “whereKey(…)” it is the same as “whereIn(….)” ?

@bakerkretzmar
Copy link
Contributor Author

bakerkretzmar commented Apr 9, 2025

In this case it's more or less the same yeah:

public function whereKey($id)
{
if ($id instanceof Model) {
$id = $id->getKey();
}
if (is_array($id) || $id instanceof Arrayable) {
if (in_array($this->model->getKeyType(), ['int', 'integer'])) {
$this->query->whereIntegerInRaw($this->model->getQualifiedKeyName(), $id);
} else {
$this->query->whereIn($this->model->getQualifiedKeyName(), $id);
}
return $this;
}
if ($id !== null && $this->model->getKeyType() === 'string') {
$id = (string) $id;
}
return $this->where($this->model->getQualifiedKeyName(), '=', $id);
}

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.

3 participants