I'm trying to filter the users from a specific user group in a component in PHP. First, I'm search for a specific user based on a keyword, next I want to test if this user belongs to one specific group 'mygroupcode'. Ideally these two statements should be combined.
I've tried the following:
public function onSelect() {
$s2_opts = [];
foreach(User::where('surname', 'LIKE', '%'.Input::get('q').'%')->orWhere('name', 'LIKE', '%'.Input::get('q').'%')->get() as $user) {
if($user->groups()->whereName('mygroupcode')->exists()) {
array_push($s2_opts, array('id' => $user->id, 'text' => $user->name . ' ' . $user->surname));
}
}
return json_encode($s2_opts);
}
This throws a MySQL-error. Something like table mydb.groups
not found... In the database, it should look for user_groups instead.
Also, I've tried:
public function onSelect() {
$s2_opts = [];
foreach(User::where('surname', 'LIKE', '%'.Input::get('q').'%')->orWhere('name', 'LIKE', '%'.Input::get('q').'%')->get() as $user) {
if(in_array('mygroupcode',array_keys($user->groups))) {
array_push($s2_opts, array('id' => $user->id, 'text' => $user->name . ' ' . $user->surname));
}
}
return json_encode($s2_opts);
}
This also yields a MySQL error. Similar, also about table groups
not being found.
Also, I've noticed there's a method called inGroup(), but this yields the same error...
public function onSelect() {
$s2_opts = [];
foreach(User::where('surname', 'LIKE', '%'.Input::get('q').'%')->orWhere('name', 'LIKE', '%'.Input::get('q').'%')->get() as $user) {
if($user->inGroup('mygroupcode')) {
array_push($s2_opts, array('id' => $user->id, 'text' => $user->name . ' ' . $user->surname));
}
}
return json_encode($s2_opts);
}
What am I doing wrong? Thanks in advance for your time.
Please note the following related questions. They are regarding backend user lists, I am looking to filter down frontend users.