3 Models that contain this trait, have the ability to be apart of a group.
5 There's many helpful methods to assist you in all of the operations related to group membership, let's get started!
9 To retrieve the groups that a model is apart of, call the `getGroups()` method:
12 $user = $provider->search()->users()->find('jdoe');
14 $groups = $user->getGroups();
16 foreach ($groups as $group) {
18 $group->getCommonName(); // ex. 'Accounting'
23 We can also pass in specific fields we need from the returned groups to speed up our queries.
25 For example, if we only need the groups common name:
28 // Group models will be returned with only their common name.
29 $groups = $user->getGroups(['cn']);
32 However, calling `getGroups()` will only retrieve the models immediate groups (non-recursive).
34 To retrieve nested groups, pass in `true` into the second parameter:
37 $groups = $user->getGroups([], $recursive = true);
40 ## Retrieve Group Names
42 If you only want the models group names, call the `getGroupNames()` method:
45 $names = $user->getGroupNames();
47 foreach ($names as $name) {
49 echo $name; // ex. 'Accounting'
54 However, this method will also retrieve only the immediate groups names
55 much like the `getGroups()` method. You'll need to pass in `true` in
56 the first parameter to retrieve results recursively.
59 $names = $user->getGroupNames($recursive = true);
62 ## Checking if the Model is apart of a Group
64 To check if a model is apart of a certain group, use the `inGroup()` method:
67 $group = $provider->search()->groups()->find('Office');
69 if ($user->inGroup($group)) {
76 You can also check for multiple memberships by passing in an array of groups:
79 $groups = $provider->search()->findManyBy('cn', ['Accounting', 'Office']));
81 if ($user->inGroup($groups->toArray()) {
83 // This user is apart of the 'Accounting' and 'Office' group!
88 > **Note**: Much like the other methods above, you'll need to provide a `$recursive`
89 > flag to the `inGroup()` method if you'd like recursive results included.
91 We can also provide distinguished names instead of Group model instances:
95 'cn=Accounting,ou=Groups,dc=acme,dc=org',
96 'cn=Office,ou=Groups,dc=acme,dc=org',
99 if ($user->inGroup($dns, $recursive = true)) {
106 Or, we can also just provide the name(s) of the group(s).
114 if ($user->inGroup($names, $recursive = true)) {
123 To add the model to a specific group, call the `addGroup()` method:
126 $group = $provider->search()->groups()->find('Accounting');
128 // You can either provide a Group model:
129 if ($user->addGroup($group)) {
136 if ($user->addGroup('cn=Accounting,ou=Groups,dc=acme,dc=org')) {
143 > **Note**: You do not need to call the `save()` method for adding / removing groups.
144 > This is done automatically so you can perform clean `if` statements on the method.
148 To remove the model from a specific group, call the `removeGroup()` method:
151 $group = $user->getGroups()->first();
153 // You can either provide a Group model:
154 if ($user->removeGroup($group)) {
161 if ($user->removeGroup('cn=Accounting,ou=Office Groups,dc=acme,dc=org')) {