我试图从两个表中获取记录。
1 - 代币2 - 交易
在两个表中都有相同的列user_id
和tokens
表有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
,有人可以指导我为什么查询没有得到记录。我想要感激。
因为你从来没有->get();
的结果! $earnedData
变量现在包含已解析的查询,因为您从未执行它。
$earnedData = Token::whereIn('user_id', $descendants)
->whereHas('transaction', function($q) {
$q->where('custom', '!=', 'BB')->orWhereNull('custom');
})->sum('amount')
->get();