我有一個查詢,我寫了一個查詢資料的查詢
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();