I have two many-to-many relationships, a users table and other roles
And I have a user with 3 loaded roles in the database, but when I search, it only brings me 1 role.
class RoleModel extends BaseModel {
protected $table = 'role';
protected $fillable = ['role'];
protected $dates = ['deleted_at'];
public function usuarios() {
return $this->belongsToMany('Moltareas\Usuario\UsuarioModel', 'role_x_usuario', 'idUsuario','idRole')
->withTimestamps();
}
}
class UsuarioModel extends Authenticatable {
use Notifiable;
protected $table = 'usuario';
protected $fillable = [ 'nombre', 'apellido', 'email', 'password', 'idRole'];
protected $hidden = [ 'password', 'remember_token'];
protected $dates = ['deleted_at'];
public function roles() {
return $this->belongsToMany('Moltareas\Role\RoleModel', 'role_x_usuario', 'idRole', 'idUsuario')
->withTimestamps();
}
}
And when I bring the user roles I do:
introducir el código aquí
$user = Moltareas\Usuario\UsuarioModel::get();
foreach ($user->roles as $role) {
var_dump($role);
}
And this only shows me that the user only has 1 role, when in fact he has 3 charged.
If I make a dd ($ role) within the foreach, it shows me this
#attributes: array:5 [▼
"id" => 1
"role" => "ADMINISTRADOR"
"deleted_at" => null
"created_at" => "2016-10-07 09:57:27"
"updated_at" => "2016-10-07 09:57:27"
]