Php 为什么查询DEO在有行的情况下不获取结果?

Php 为什么查询DEO在有行的情况下不获取结果?,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我正试图从两个表中取出记录 1-代币 2-交易 在这两个表中都有相同的列user\u id和tokens表中有transaction\u id。我正在尝试获取与令牌相关的所有事务。我在试着跟随 $EarnedData = Token::whereIn('user_id', $descendants) ->whereHas('transaction', function($q){ $q->where('custom', '!=', 'BB')->o

我正试图从两个表中取出记录

1-代币 2-交易

在这两个表中都有相同的列
user\u id
tokens
表中有
transaction\u id
。我正在尝试获取与
令牌
相关的所有事务。我在试着跟随

$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))
调试之后,我从上面的脚本中得到了原始查询,下面是

$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
,有人能告诉我为什么查询没有得到记录吗。我很感激。

因为你永远不会
->-get()结果!
$earnedData
变量现在包含已解析的查询,因为您从未执行过它

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

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

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