Php 获取laravel 5.2中不属于特定组的所有用户

Php 获取laravel 5.2中不属于特定组的所有用户,php,laravel-5.2,Php,Laravel 5.2,我已经搜索并尝试了很多,但不幸的是,我没有解决我的问题。所以,我把我的问题贴在这里,请看一下并给我一些解决方案 我很少有表来管理用户,比如用户,配置文件,组,以及组用户。现在,我想检索不是特定组成员的所有用户名 努力 $users = DB::table('users') ->join('profiles', 'users.id', '=', 'profiles.user_id') ->join('group_user AS gu', 'gu.user_id', '!=', 'use

我已经搜索并尝试了很多,但不幸的是,我没有解决我的问题。所以,我把我的问题贴在这里,请看一下并给我一些解决方案

我很少有表来管理用户,比如
用户
配置文件
,以及
组用户
。现在,我想检索不是特定组成员的所有用户名

努力

$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '!=', 'users.id')
->join('groups', 'groups.id', '=', 'gu.group_id')
->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
我正在执行上面的查询,以获取不是特定组成员的用户列表,但直到现在我还无法解决它。 如果我更改
!=to=
然后我得到特定组中所有用户的结果

图像中的表记录和结构


如图所示,您可以看到我有5个用户,其中3个用户拥有管理组,2个用户没有。如果我为管理组运行查询,那么应该有剩余的2个用户,或者如果我为测试组运行查询,那么我应该有5个用户。

您不需要更改
连接部分,您只需要在
where()
部分中更改或添加所有用户

$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '=', 'users.id') // mark it as '='
->join('groups', 'groups.id', '=', 'gu.group_id')
->where('groups.label', '<>', $grouplabel) // change here, see below description
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
编辑2

    $users = DB::table('users')
    ->join('profiles', 'users.id', '=', 'profiles.user_id')
    ->join('group_user AS gu', 'gu.user_id', '=', 'users.id')
    ->join('groups', 'groups.id', '=', 'gu.group_id'); 

//if users exists in specific group
if($users->where('groups.label', '=', $grouplabel)->count() > 0 ){
   $result = $users->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');

} // return all users 
else{
      $result = $users->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
}

我什么都没得到,你什么都没得到?查询结果还是我的答案?结果,我得到的是空[]数组。如果我复制了查询并粘贴到phpmyadmin sql中,那么我也会得到空的结果集。您能给我显示普通查询以便我查看查询吗?“选择CONCAT_WS(“”,profiles.firstname,profiles.lastname)作为名称,
users
id
作为
id
来自
users
internal-join
users
id
配置文件
用户id
内部连接
组用户
AS
gu
关于
gu
用户id
内部连接
用户de>组
组上
id
=
组id
其中
标签
?”
    $users = DB::table('users')
    ->join('profiles', 'users.id', '=', 'profiles.user_id')
    ->join('group_user AS gu', 'gu.user_id', '=', 'users.id')
    ->join('groups', 'groups.id', '=', 'gu.group_id'); 

//if users exists in specific group
if($users->where('groups.label', '=', $grouplabel)->count() > 0 ){
   $result = $users->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');

} // return all users 
else{
      $result = $users->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
}