mercredi 26 avril 2017

Can't load belongsToMany relationship

I use jenssegers/laravel-mongodb and have 2 models : User and Role with below relation:

User :

public function roles(){
    return $this->belongsToMany(Role::class,null,'role_id');
}

Role :

public function users() {
    return $this->belongsToMany(User::class,null,'user_id');
}

My database :

users

_id:59016aac35623e1b48007e93
email:"test2@bigin.vn"
password:"123"
updated_at:2017-04-27 10:51:08.000
created_at:2017-04-27 10:51:08.000
role_id: Array
    0:"58eeffdd7cab91da5f30da24"

roles :

_id:58eeffdd7cab91da5f30da24
slug:"user"
name:"User"
permissions:"{"dashboard.index":true}"
updated_at:2017-04-26 22:29:54.000

When I want to role attached to user model when I load user, I use this code :

User::with('roles')->find("59016aac35623e1b48007e93")->roles

But the results is empty ? I'm very appreciate if found solution for this problem !



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire