10

I'm trying to setup some simple favoriting for my app using Laravel/Jenssegers MongoDB

I have two models:

class Item {

    public function favorites()
    {
       return $this->hasMany('App\Models\User', 'favorites');
    }
}

class User {

    public function favorites()
    {
       return $this->hasMany('App\Models\Item', 'favorites');
    }
}

So an item can have many users and a user can have many items or "favorites".

When I try to run a simple query like:

Item::with('favorites')->get();

The relationship for favorites is there but an empty array.

The favorites table is simple and just has three columns:

_id, item_id, user_id

I've also tried using embedsMany with no luck:

return $this->embedsMany('App\Models\User', 'favorites');

I've also tried every combination of parameters.

return $this->embedsMany('App\Models\User', 'favorites', 'building_id', 'user_id');
return $this->embedsMany('App\Models\User', 'favorites', 'user_id', 'building_id');
return $this->hasMany('App\Models\User', 'favorites', 'building_id', 'user_id');
return $this->hasMany('App\Models\User', 'favorites', 'user_id', 'building_id');

Strange results when using:

return $this->hasMany('App\Models\User', 'favorites', 'building_id', 'user_id');

Only in this situation it returns ALL users, even though favorites has only one record.

Also trying to debug using getQueryLog just gives me an empty array at all times.

print_r(\DB::connection('mongodb')->getQueryLog());

I've done this using mysql and have never had issues. So not really sure where the issues are coming from.

Rob
  • 10,851
  • 21
  • 69
  • 109
  • You must keep in mind that NoSQL databases like MongoDB not supposed to have relationships. That's the idea of NoSQL database. If you need traditional relationship maybe you should consider using traditional SQL database (like MySQL or PostgreSQL). – Bald Apr 06 '16 at 06:13
  • Exacty, if you want hasMany relation, you have to nest the entire users instances inside Item object for example `{ "users": [ {"name": "user1"}, {"name": "user2"} ] }` – Claudio King Apr 06 '16 at 07:31
  • @Bald I guess eloquent doesn't have joins, it' uses eager loading. So what's wrong with fetching some associated data. For instance you're not gonna store the user object inside of ever comment of a post right? – Rob Apr 06 '16 at 08:49
  • @Rob If the query log is empty, you might've forgotten to enable logging. Did you try calling the [`enableQueryLog()`](https://laravel.com/api/5.2/Illuminate/Database/Connection.html#method_enableQueryLog) before trying to retrieve the log itself? – Kemal Fadillah Apr 20 '16 at 19:14
  • I am not really using MongoDb but this seems like a ```hasManyThrough``` in standard relational Eloquent ! – BeS Apr 20 '16 at 23:21
  • None of the relations seem to be working. Even trying something like `hasOne('user')`. – Rob Apr 21 '16 at 02:18

1 Answers1

4

Can you try these instead, I know its the same thing but might do the trick :

use App\User;
use App\Item;

class Item {

    public function favorites()
    {
       return $this->hasMany('User');
    }
}

class User {

    public function favorites()
    {
       return $this->hasMany('Item');
    }
}

class Favourite {

        public function users()
        {
           return $this->belongsTo('User');
        }

        public function items()
        {
           return $this->belongsTo('Item');
        }
    }

Also add inverse of the above relations like: belongsTo. and try this query then :

//Just for testing purpose, if raw query returns right result
$items = DB::table('items')
->join('favorites','items.id','=','favorites.id')
->get();
Murlidhar Fichadia
  • 2,589
  • 6
  • 43
  • 93