Mysql 将SQL查询转换为雄辩的

Mysql 将SQL查询转换为雄辩的,mysql,eloquent,Mysql,Eloquent,我一直坚持将这个sql查询转换为雄辩的。我已经尝试了相当多,但雄辩的结果并没有以我想要的形式返回,即: Vuln A - Host 1 - Host 2 Vul B - Host 3 Vul C - Host 1 - Host 2 - Host 5 这些是我的模型 模型(多对多) 询问 我在mysql中有以下SQL提取 SELECT * from Vuln INNER JOIN Host_Vuln on Host_Vuln.Vuln_id = Vuln.id INNER JO

我一直坚持将这个sql查询转换为雄辩的。我已经尝试了相当多,但雄辩的结果并没有以我想要的形式返回,即:

Vuln A
 - Host 1
 - Host 2

Vul B
 - Host 3

Vul C
 - Host 1
 - Host 2
 - Host 5
这些是我的模型

模型(多对多) 询问 我在mysql中有以下SQL提取

SELECT * from Vuln 
INNER JOIN Host_Vuln on Host_Vuln.Vuln_id = Vuln.id
INNER JOIN Host on Host_Vuln.Host_id = Host.id
WHERE (Host.country = 1) AND (Host.apptype like 'Mob%')
ORDER BY Vuln.severity DESC
但我被困在雄辩的。。。这就是我所拥有的

  $vulnResult = DB::table('vulns')
    ->join('host_vuln', 'vulns.id', '=', 'host_vuln.finding_id')
    ->join('hosts', 'host_vuln.host_id', '=', 'hosts.id')
    ->where('hosts.country', 1)
    ->where('hosts.apptype', 'like', 'Web%')
    ->orderBy('vulns.score', 'DESC')
    ->get();
结果在集合中返回,而不在Vuln下嵌套主机。生成的集合是一个普通数组,其中每个主机都映射到一个Vuln,即使一个Vuln有多个主机。这是我想消除的低效重复


我想要的最终结果是每个Vuln可以有许多主机。如果没有主机,那么就不要显示Vuln。

我真是个笨蛋。下面的内容非常完美(尽管我仍然没有理解;为什么它会起作用)


我认为有打字错误的问题。vuln\u主机应该是主机\u vuln尝试这个“$vulns=DB::table('vulns')->join('host\u vulns','vulns.id','=','host\u vulns.finding\u id')->join('hosts','vuln\u host.host\u id','=','hosts.id')->where('hosts.apptype','like','Mob%')->orderBy('vulns.severity','DESC get()`嘿,谢谢!我已经测试过了,但还是不行
  $vulnResult = DB::table('vulns')
    ->join('host_vuln', 'vulns.id', '=', 'host_vuln.finding_id')
    ->join('hosts', 'host_vuln.host_id', '=', 'hosts.id')
    ->where('hosts.country', 1)
    ->where('hosts.apptype', 'like', 'Web%')
    ->orderBy('vulns.score', 'DESC')
    ->get();
    $filter = function ($w) { 
        $w->where('country', 1)
          ->where('apptype', 'like', 'Mob%'); 
    };

    $findings1 = Vuln::with(['hosts' => $filter])
                    ->whereHas('hosts', $filter)
                    ->get();