Laravel-mongodb: Exception when applying select to return just couple fields through belongsToMany relation

Created on 7 Dec 2015  路  2Comments  路  Source: jenssegers/laravel-mongodb

I have some belongsToMany relations where I want to return just couple fields using select:

public function followedUsers()
{
    return $this->belongsToMany('App\Models\User',null,'followers','followed_users')
                        ->select('_id','slug','profile_name','profile_image');
}

When I query the relation
\App\Models\Users::with('followedUsers)->find($id);

I get an exception ""Invalid argument supplied for foreach()"

If I remove the "select" from the relation it works fine but the models are pretty big so I would send a lot of useless data through the API.

It seems this happens only for belongsToMany, I did not notice this problem for 1 to many relations at least not in 1 the direction

Most helpful comment

Any update on this?

All 2 comments

@jenssegers I tested some more relation and noticed the following:

  • for hasMany if I apply the "select" it does not generate exception but it returns an empty array.
  • for belongsTo the "select" fields seems to work properly.

Any update on this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastiaanluca picture sebastiaanluca  路  3Comments

YSimple picture YSimple  路  3Comments

tomartailored picture tomartailored  路  3Comments

kschethan picture kschethan  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments