为什么查询不提取结果但有行?

问题描述 投票:1回答:1

我试图从两个表中获取记录。

1 - 代币2 - 交易

在两个表中都有相同的列user_idtokens表有transaction_id。我想要获得与token有关的所有交易。我正在努力追随

$EarnedData = Token::whereIn('user_id', $descendants)
    ->whereHas('transaction', function($q){
            $q->where('custom', '!=', 'BB')->orWhereNull('custom');
    })->sum('amount');

调试之后,我从上面的脚本得到了原始查询

    select sum(`amount`) as aggregate from `tokens` 
    where 
    `user_id` in (15, 49, 58,117, 119, 120, 130, 13) 
    and exists 
    (select * from `transactions` where `tokens`.`transaction_id` = `transactions`.`id`
    and (`custom` != 'BONUS' or `custom` is null))

当我执行查询时,我得到了NULL,有人可以指导我为什么查询没有得到记录。我想要感激。

php mysql laravel laravel-5
1个回答
0
投票

因为你从来没有->get();的结果! $earnedData变量现在包含已解析的查询,因为您从未执行它。

$earnedData = Token::whereIn('user_id', $descendants)
    ->whereHas('transaction', function($q) {
        $q->where('custom', '!=', 'BB')->orWhereNull('custom');
    })->sum('amount')
    ->get();
© www.soinside.com 2019 - 2024. All rights reserved.