Reputation: 89
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".
Upvotes: 0
Views: 518
Reputation: 12188
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
Upvotes: 0
Reputation: 1769
Well you can write
whereNotIn
for this case. For quick answer I am assuming that you have a database field which isgroup_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"
Upvotes: 0