I have 2 tables and there is data for users. I want to take the data between the specified dates from these 2 tables, add them together if the same user is in the 2 tables, and write them as a single data if not. I made a join operation as follows, but the unionAll method combines the data in the 2 SQL results, but does not collect the same user data. For example, the user with id 1 is recorded in a table as publisher_id and the column name I want to collect is ticket. In the other table, there is a record as user_id and the column name I want to collect is diamonds. If the ticket value is 10 in one table and the diamonds value is 20 in the other table, the result should be 30, but two separate data are coming. How do I do this merging using the paginate management?
public function index(Request $request)
{
$begin_date = $request->get('begin_date') ? date('Y-m-d 00:00:00', strtotime($request->get('begin_date'))) : now()->startOfMonth();
$end_date = $request->get('end_date') ? date('Y-m-d 23:59:59', strtotime($request->get('end_date'))) : now()->endOfMonth();
$begin_time = strtotime($begin_date);
$end_time = strtotime($end_date);
$a_recharges = ARecharge::select('publisher_id as user_id', DB::raw('SUM(ticket) as coins'))
->whereBetween('created_at', [$begin_date, $end_date])
->groupBy('publisher_id');
$b_recharges = BRecharge::select('user_id', DB::raw('SUM(diamonds) as coins'))
->whereBetween('create_time', [$begin_time, $end_time])
->groupBy('user_id');
$recharges = $a_recharges->union($b_recharges)
->orderByDesc('coins')
->paginate(10)
->appends($request->except('page'));
return view('admin.publisherrecharges.index', compact('recharges'));
}
I tried the unionAll method but the results are not what I wanted
ibrahim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
0