Skip to content

Help with filtering groups and results #490

Answered by stevebauman
shark2k asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @shark2k,

When I changed the value for whereContains so it will not find any groups, instead of getting the output of "Not in group" I just get a blank page.

This is because the result of the query is an empty collection, which will not be iterated over.

For example, this script will not call die():

$foo = [];

foreach ($foo as $bar) {
    die('This will not be reached.');
}

If you're looking to check whether a user has at least one group matching the query, you can instead use exists():

use LdapRecord\Models\ActiveDirectory\User;

$user = User::find('cn=John Doe,ou=Users,dc=local,dc=com');

if ($user->groups()->whereContains('cn', 'Accounting')->exists()) {
    // The user is apart …

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@shark2k
Comment options

@stevebauman
Comment options

@shark2k
Comment options

@stevebauman
Comment options

Answer selected by shark2k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants