Skip to content

[Proposal] $query->whereFieldExists #82

@alexandre-butynski

Description

@alexandre-butynski
Contributor

Sometimes, I have to use this syntax :

User::whereRaw(array('programs_ids.'.$program_id => array('$exists' => true)));

It could be better if it was :

User::whereExists('programs_ids.'.$program_id);

But a whereExists() method already exists in Laravel. It corresponds to the WHERE EXISTS SQL operator, wich have no sens in the MongodDB context, am I right ? It could be good to overwrite the initial method with an other one, matching the $exists MongoDB operator like in the case above but it's impossible because the arguments are not the same (the initial method need a Closure as first argument).

We could have a whereFieldExists() method with an implementation like that :

public function whereFieldExists($column, $exists = true, $boolean = 'and')
{
    return $this->whereRaw(array($column => array('$exists' => $exists)), array(), $boolean);
}

public function whereFieldNotExists($column, $boolean = 'and')
{
    return $this->whereFieldExists($column, false, $boolean);
}

After tests and writing this I'm not completely sure that this syntax is really great... What do you think about ? this question is more general because we could have also whereType or whereAll. I could work on this in the next days.

Activity

jenssegers

jenssegers commented on Dec 7, 2013

@jenssegers
Contributor

I could just overwrite the whereExists method, or does this mean something different in MySQL?

alexandre-butynski

alexandre-butynski commented on Dec 8, 2013

@alexandre-butynski
ContributorAuthor

Yes, it means something different (http://dev.mysql.com/doc/refman/4.1/en/exists-and-not-exists-subqueries.html) and a method already exists. We can't override it because the first argument has to be a Closure.

jenssegers

jenssegers commented on Dec 8, 2013

@jenssegers
Contributor

And I guess whereNotNull does not give the same results.

alexandre-butynski

alexandre-butynski commented on Dec 8, 2013

@alexandre-butynski
ContributorAuthor

In fact, whereNotNull give something very close that {$exists: true}. The only difference is were an index exists but has a null value... but it doesn't matter in my use case. It look like a side effect but, in my particular case, it does the job for the moment. Thanks to lead me to this solution.

In a more general discussion, what do you think about writing custom methods for specific MongoDB operators ($type, $exists, $all, $size...) ?

jenssegers

jenssegers commented on Dec 8, 2013

@jenssegers
Contributor

I will take a look at it, should not be that hard I guess.

alexandre-butynski

alexandre-butynski commented on Dec 8, 2013

@alexandre-butynski
ContributorAuthor

Ok, ask if you need help.

jenssegers

jenssegers commented on Dec 8, 2013

@jenssegers
Contributor
alexandre-butynski

alexandre-butynski commented on Dec 9, 2013

@alexandre-butynski
ContributorAuthor

Great !

NominationP

NominationP commented on Aug 19, 2018

@NominationP

How to solve this

NominationP

NominationP commented on Aug 19, 2018

@NominationP

$query->whereExists(function($que){
$que->select(1)->from('bargain_coupons')->whereRaw('bargain_coupons.shop_id=id');
});

--! help

added a commit that references this issue on Sep 2, 2024
efbf965
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jenssegers@alexandre-butynski@NominationP

        Issue actions

          [Proposal] $query->whereFieldExists · Issue #82 · mongodb/laravel-mongodb