db::raw 添加在查询中为空

db::raw adding is null in query

我正在尝试使用 laravel 查询生成器,例如:-

$users = DB::table('baid_collection')
         ->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
         ->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
         ->where(DB::raw("assigned_user_id = '$userId' and DATE(date_entered)=CURDATE()"))
         ->groupBy('assigned_user_id')
         ->get();

这个查询应该是这样的

select sum(total) as total_collect,collection_limit_c from `baid_collections` inner join `users_cstm` on `baid_collections`.`assigned_user_id` = `users_cstm`.`id_c` where assigned_user_id = '15426608-3ea5-f299-7a80-601bd06be2d9' and DATE(date_entered)=CURDATE() group by `assigned_user_id`

但最后 运行 查询显示我

 select sum(total) as total_collect,collection_limit_c from `baid_collection` inner join `users_cstm` on `baid_collections`.`assigned_user_id` = `users_cstm`.`id_c` where assigned_user_id = '15426608-3ea5-f299-7a80-601bd06be2d9' and DATE(date_entered)=CURDATE() is null group by `assigned_user_id`

在查询中is null我不想要的问题在查询中为空

检查这个是否有帮助,在原始查询中写 whereDate 而不是添加日期。 如果你不使用Carbon,你也可以使用php date("Y-m-d"),方法来获取日期

DB::table('baid_collection')
                ->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
                ->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
                ->where(DB::raw("assigned_user_id = '$userId'"))
                ->whereDate("date_entered", "=", Carbon::now()->toDateString())
                ->groupBy('assigned_user_id')->get();

你可以试试这个

$users = DB::table('baid_collection')
         ->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
         ->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
         ->where(DB::raw("assigned_user_id = '$userId' and DATE(date_entered) = '".date('Y-m-d')."'"))
         ->groupBy('assigned_user_id')
         ->get();

使用 whereRaw 而不是 where like

$users = DB::table('baid_collections')
         ->select(DB::raw('sum(total) as total_collect,collection_limit_c'))
         ->join('users_cstm', 'baid_collections.assigned_user_id', '=', 'users_cstm.id_c')
         ->whereRaw("assigned_user_id = '{$userId}' and DATE(date_entered)=CURDATE()")
         ->groupBy('assigned_user_id')
         ->get();

由于 where 至少需要两个参数而您只传递一个参数,这会造成问题