null在表laravel 5.8中没有数据时,返回sum的intead为0

mepcadol  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(468)

我的问题

$summary['total_cash'] = DB::table('wallets')
                                  ->select(DB::raw('SUM(amount) as total_cash'))
                                  ->where('payment_details','affiliate product purchase commission')
                                  ->where('user_id',$group_id)
                                  ->first()->total_cash;

我得到的错误https://prnt.sc/sjllwv
这些表中都没有可用的数据
我要返回0
如果有任何打字错误,请忘记
谢谢您

kkbh8khc

kkbh8khc1#

如果数据库中的amount字段默认为空,请尝试以下操作

$summary['total_cash'] = DB::table('wallets')
                         ->select(DB::raw('coalesce(SUM(amount),0) as total_cash'))
                         ->where('payment_details','affiliate product purchase commission')
                         ->where('user_id',$group_id)
                         ->first()->total_cash

相关问题