Laravel custom or extended belongsToMany

68 views Asked by At

I have a table 'users' and a table 'groups'. In the application, i am working on, the administrator can assign groups to users. So there is a many-to-many relationship between these tables.

With the following code i defined this relation:

public function groups()
{
    return $this->belongsToMany('\App\Group');
}

This way i can retrieve the groups this user is assigned to:

$users = User::with('groups')->get()

But above this, settings in the application provide the posibility to set a group for all the users. Is it possible to define a relation so that i retrieve all groups that are related to the user plus the one that is assigned to all users according to the setting? Something like this:

public function groupsInclDefaultGroup()
{
    $defaultGroupId = Setting::getSetting('defaultGroupId');
    return $this->belongsToMany('\App\Group')->..??..($defaultGroupId);
}

.

$users = User::with('groupsInclDefaultGroup')->get();
0

There are 0 answers