It does not work that way. You most likely(I may be wrong) can't pass usersGroup.group.name
to the query as this way you will try to make statement like SELECT x,y,z, usersGroup.group.name FROM users
which will fail.
I don't see the point of creating UserGroup model. Instead use belongsToMany
relationship like:
//User
public function groups(){
return $this->belongsToMany('Group', 'users_groups', 'user_id', 'group_id');
//first parameter is model name, second is pivot table, third is foreignKey column name and 4th is localKey column name
}
This way when you call $user->groups()->get()
you will get all the Group models that user belongs to through the pivot table.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community