我有两个查询来自两个不同的表。第一个包含我想在第二个查询中使用的ID。现在我这样做。我将ID提取到一个新数组中,然后在第二个查询中使用->whereIn()
中的该数组。如何将ID数组从一个Laravel查询传递到另一个
$campaign = DB::connection('mysql2')->table('mc_spots')
->select('s_customer', 's_product', 'spotid')
->where('s_starttermin', '>=', date("Y-m-d", strtotime($dateFrom)))
->where('s_lastrun', '<=', date("Y-m-d", strtotime($dateUntil)))
->where('s_media', '=', $media)
->where(function ($query) use ($products) {
for ($i = 0; $i < count($products); $i++) {
$query->orwhere('s_product', '=', $products[$i]);
}
})
->get();
$campaignID = [];
foreach ($campaign as $c) {
array_push($campaignID, $c->spotid);
}
$table = DB::connection('mysql2')->table('schaltdaten_tv_de')
->select('*')
->join('epgdata_channel', 'schaltdaten_tv_de.cid', '=', 'epgdata_channel.channelid')
->join('mc_spots', 'schaltdaten_tv_de.ReferenceDescription', '=', 'mc_spots.spotid')
->whereIn('ReferenceDescription', $campaignID)
->groupBy('epgdata_2013.id')
->orderBy('StartofBreak', 'ASC')
->limit(500)
->get();
有没有更方便的方式来做到这一点没有通过$campaign
每个项目的循环?
您的问题中的代码只使用Laravel的流利的查询生成器,而不是雄辩。 –
好的。我编辑了我的问题。 Thx – harunB10
您使用的是什么版本的Laravel? –