0

I have a query where I'm returning an awful lot of rows, so need to limit which fields I'm calling back, so I'm using select and a where method.

However I also would like to limit it further by ignoring those in certain groups.

I currently have

$users = Adldap::search()
            ->select('extensionattribute2', 'mail', 'samaccountname')
            ->where('company', '=', $company)
            ->get();

Can someone help me add a where clause so that I can only select users from that aren't in a selection of 4 groups.

EG. Select all users, but not those in "Accounts", "HR", "Admins".

Fred
  • 69
  • 9

2 Answers2

0

Well you can write whereNotIn for this case. For quick answer I am assuming that you have a database field which is group_name which includes "Accounts", "HR", "Admins". It's just for clarifying to you. You can change your database field name.

$users = Adldap::search()
        ->select('extensionattribute2', 'mail', 'samaccountname')
        ->where('company', '=', $company)
        ->whereNotIn('group_name', ["Accounts", "HR", "Admins"])
        ->get();

This query will return all users ignoring "Accounts", "HR", "Admins"

Akhtar Munir
  • 1,691
  • 3
  • 11
  • 28
0

use whereDoesntHave:

$unwantedGroups=["Accounts", "HR", "Admins"];
$users=Adldap::search()->whereDoesntHave('groups',function (Builder $query) use($unwantedGroups){
    $query->whereIn('groupName', $unwantedGroups)
})->select('extensionattribute2', 'mail', 'samaccountname')
->where('company', '=', $company)->get();

please be careful about relation name between user & groups 'groups or group'.

more details in: https://laravel.com/docs/7.x/collections#method-wherein

OMR
  • 11,736
  • 5
  • 20
  • 35
  • This also didn't work for me, tries to search the field "doesnt_have" array:3 [ "field" => "doesnt_have" "operator" => "=" "value" => "\64\69\73\74\69\6e\67\75\69\73\68\65\64\6e\61\6d\65" ] – Neo Oct 06 '21 at 14:28