mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

BelongsToMany uses wrong query to retrieve members #328

Closed obahareth closed 6 years ago

obahareth commented 9 years ago

I have the following classes:

<?
class User extends Moloquent
{
    protected $collection = 'users';

    public function challengesWon()
    {
        return $this->belongsToMany('Challenge', null, 'winning_users', 'challenges_won');
    }
}

class Challenge extends Moloquent
{
    protected $collection = 'challenges';

    public function winningUsers()
    {
        return $this->belongsToMany('User', null, 'challenges_won ', 'winning_users');
    }
}

When I try to do:

$challenge->winningUsers()->get();

I get 0 results. I went in and checked the queries that have been run using barryvdh's Laravel Debug Bar and I found that the query being run is this:

users.find({"challenges_won ":"53fd9e0175cde71e158b4567"},[])

The correct query to run would be this:

users.find({"challenges_won":{$in : ["53fd9e0175cde71e158b4567"]}})
irving-caamal commented 4 years ago

did you solved this?