'How can I get all counters transactions total fare and total ticket in every individual date?

Hello Laravel developers, I have two table counters and transactions. Where transactions table has counters foreign key (from_counter).

counters table:

id | counter_name
1  | Counter 1
2  | Counter 2

transactions table:

id | from_counter (counter id) | fare | date
1  |  1                        | 50   | 2022-04-01
2  |  1                        | 40   | 2022-04-01
3  |  2                        | 40   | 2022-04-01
4  |  2                        | 50   | 2022-04-01
5  |  1                        | 50   | 2022-04-02
6  |  1                        | 40   | 2022-04-02
7  |  2                        | 40   | 2022-04-02
8  |  2                        | 50   | 2022-04-02

I want to show all counters with its total daily ticket and total fare date wise in my report.

My desired output something like this:

 ----------
    Date       |   Counter 1           |    Counter 2       |   Counter 3          | Total
    

----------
2022-04-01 |   100 (total tickets) | 50 (total tickets) |    0 (total tickets)     | ....
               500 (total fare)      300 (total fare)        0 (total fare)


----------
 2022-04-02 |   50 (total tickets) | 80 (total tickets) |    20 (total tickets)    | ....
                300 (total fare)     600 (total fare)        250 (total fare)


----------
 2022-04-03 |   100 (total tickets) | 50 (total tickets) |    100 (total tickets)    | ....
                500 (total fare)      300 (total fare)     1250 (total fare)

I am trying to apply some query but my desired output data not return here

$query = Counter::select('id', 'counter_name', 'username', 'mobile')->with(['transactions' => function($q) use($request) {
            $q->select('id', 'from_counter', 'date', DB::raw("SUM(fare) AS total_fare, COUNT(id) AS total_ticket"));

            if ($request->start_time !== null && $request->end_time !== null) {
                $q->whereDate('date', '>=', $request->start_time)->whereDate('date', '<=', $request->end_time);
            }

            $q->groupBy('from_counter', DB::raw('DATE(date)'));
        }]);

        $data = $query->get();


        return $data;

Output is:

    [
{
"id": 1,
"counter_name": "Counter 1",
"transactions": [
{
"id": 1,
"from_counter": 1,
"date": "2022-03-31 05:54:32",
"total_fare": "570",
"total_ticket": 13
},
{
"id": 27,
"from_counter": 1,
"date": "2022-04-01 08:50:05",
"total_fare": "100",
"total_ticket": 2
},
{
"id": 30,
"from_counter": 1,
"date": "2022-04-03 08:50:05",
"total_fare": "85",
"total_ticket": 2
}
]
},
{
"id": 2,
"counter_name": "Counter 2",
"transactions": [
{
"id": 6,
"from_counter": 2,
"date": "2022-03-31 06:53:41",
"total_fare": "235",
"total_ticket": 5
},
{
"id": 29,
"from_counter": 2,
"date": "2022-04-01 08:50:05",
"total_fare": "45",
"total_ticket": 1
},
{
"id": 32,
"from_counter": 2,
"date": "2022-04-03 08:55:05",
"total_fare": "40",
"total_ticket": 1
}
]
}]

I also write another query

$query  = DB::table('counters as coun')
            ->leftjoin('transactions as trans', 'coun.id', 'trans.from_counter')
            ->select('coun.id', 'coun.counter_name', 'trans.date', DB::raw("SUM(trans.fare) AS total_fare, COUNT(trans.id) AS total_ticket"))
            ->get()
            ->groupBy(function($date) {
                return Carbon::parse($date->date)->format('d-m-Y');
            });

            if ($request->start_time !== null && $request->end_time !== null) {
                $query = $query->whereBetween('trans.date', [$request->start_time, $request->end_time]);
            }

        $data = $query->get();

        return $query;

Please help me to get my desired output.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source