我有以下 SQL 查询:
SELECT * from db.tableA WHERE field in (SELECT id FROM db.tableB where other_field = value);
Run Code Online (Sandbox Code Playgroud)
我想从 tableA 中选择,其中字段位于子查询返回的值数组中。问题是:我怎样才能用 eloquent 做到这一点?我目前的解决方案(我认为这很丑陋)如下:
$a = \App\tableB::where("other_field", "=", $value)->select('id')->get();
$arr = array();
for ($i = 0; $i < count($a); $i++) array_push($arr, $a[$i]['id']);
$res = \App\tableA::whereIn("field", $arr)->get();
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
谢谢!