Below is my query using Laravel query builder:
$begin = new DateTime(\'2016-07-01\');
$end = new DateTime(\'2016-07-31\');
$startDate = $begin->format(\'Y
After much research, it seems like I have missed out this
mergeBindings($sub->getQuery())
My code:
$transaction = DB::table(DB::raw("({$deposit->toSql()}) t"))
->mergeBindings($sub->getQuery()) // this is required for selecting from subqueries
->select('user_id', DB::raw("sum(total_deposit) as total_deposit_amount, sum(total_withdraw) as total_withdraw_amount"))
->groupBy('user_id')
->get();