能否在我的模型導向請求中加入「with」查詢選項?
P粉311563823
P粉311563823 2023-09-11 00:37:19
0
1
397

我有一個查詢,我寫了一個查詢資料的查詢

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粉311563823
P粉311563823

全部回覆(1)
P粉063862561

您可以透過將history表格設定為資料透視表來實現此目標,因此查詢將如下所示。

$query = Lead::with([
             'history' => function($history) {
                $history->select(column names);
            },
            'history.offer' => function ($offer) {
              $offer => select(column names);
            }])->where('Your condition')
                 ->get();
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!