我有一个查询,我写了一个查询数据的查询
with last_sent_at as ( Select offer_id, lead_id, max(created_at) as sent_at From offer_history Group by offer_id, lead_id)
我需要将其与laravel模型系统连接起来。
所以我有三个表:leads => history => offers
我有一个请求
Lead::with([..., 'offers'])->someFunction(?)->filters()->get();
我需要从 'last_sent_at' 中获取数据,但我不知道如何做。 我尝试了子查询,但速度很慢
P粉0638625612023-09-11 18:18:08
您可以通过将history表设置为数据透视表来实现此目标,因此查询将如下所示。
$query = Lead::with([ 'history' => function($history) { $history->select(column names); }, 'history.offer' => function ($offer) { $offer => select(column names); }])->where('Your condition') ->get();