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

whereHas shorthand for nested value checking. #2052

Open
theomessin opened this issue Feb 5, 2020 · 0 comments
Open

whereHas shorthand for nested value checking. #2052

theomessin opened this issue Feb 5, 2020 · 0 comments

Comments

@theomessin
Copy link

@theomessin theomessin commented Feb 5, 2020

Instead of:

return $query->whereHas('some.very.deep.nested', function ($query) {
    $query->where('key', '>=', 2);
});

Something like:

return $query->whereHasMagic('some.very.deep.nested.key', '>=', 2);

Example implementation using Scopes:

    public function scopeWhereHasMagic($query, $relation, $value, $operator = '=')
    {
        // Split relation to relation.key
        $parts = collect(explode('.', $relation));
        $attribute = $parts->pop();
        $relation = $parts->implode('.');
        // Return constructed query.
        return $query->whereHas($relation, function ($q) use ($attribute, $operator, $value) {
            return $q->where($attribute, $operator, $value);
        });
    }

Anyone think this would be useful? Also, can anyone come up with an actual name for this method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.