Re grouping grouped collections

Posted 9 months ago by kosky2005

I have this collection array

Collection {#520 ▼
  #items: array:19 [▼
    754924 => array:4 [▼
      0 => {#441 ▼
        +"Staff_Number": "754924"
        +"opening_bal": "426.44"
      }
      1 => {#465 ▼
        +"Staff_Number": "754924"
        +"total": "390.00"
        +"transCode": "CONTRIBUTION"
      }
      2 => {#478 ▼
        +"Staff_Number": "754924"
        +"total": "136.44"
        +"transCode": "INTEREST"
      }
      3 => {#497 ▼
        +"Staff_Number": "754924"
        +"total": "-100.00"
        +"transCode": "PARTIAL WITHDRAWAL"
      }
    ]
    754928 => array:3 [▶]
    754981 => array:3 [▶]
    754989 => array:3 [▶]
    754995 => array:3 [▶]

But i want this output

Collection {#520 ▼
  #items: array:19 [▼
    754924 => array:2 [▼
      0 => {#441 ▼
        +"Staff_Number": "754924"
        +"opening_bal": "426.44"
      }
      1 => {array:3 ▼
                  0 => {#476 ▼
                    +"Staff_Number": "754924"
                    +"total": "390.00"
                    +"transCode": "CONTRIBUTION"
                  }
                  1 => {#478 ▼
                    +"Staff_Number": "754924"
                    +"total": "136.44"
                    +"transCode": "INTEREST"
                  }
                  2 => {#497 ▼
                    +"Staff_Number": "754924"
                    +"total": "-100.00"
                    +"transCode": "PARTIAL WITHDRAWAL"
                  }
    ]
    754928 => array:2 [▶]
    754981 => array:2 [▶]
    754989 => array:2 [▶]
    754995 => array:2 [▶]
    755007 => array:2 [▶]

This is my code

 $getOpenBalTotal = DB::table('memcontribution')
            ->selectRaw('memcontribution.Staff_Number,sum(memcontribution.Amount) as opening_bal')
            ->leftJoin('memberslist','memberslist.staff_id', '=', 'memcontribution.Staff_Number')
            ->where('memberslist.department_id','=',$getDept)
            ->where('memcontribution.transDate','<',$getFromDate)
            ->groupBy('memcontribution.Staff_Number')
            ->get();


        $getOpenBalDetails = DB::table('memcontribution')
            ->selectRaw('memcontribution.Staff_Number,sum(memcontribution.Amount) as total,memcontribution.code as transCode')
            ->leftJoin('memberslist','memberslist.staff_id', '=', 'memcontribution.Staff_Number')
            ->where('memberslist.department_id','=',$getDept)
            ->where('memcontribution.transDate','<',$getFromDate)
            ->groupBy('memcontribution.Code')
            ->groupBy('memcontribution.Staff_Number')
            ->get();

         $collection = new Collection;

         $all = $collection->merge($getOpenBalTotal)->merge($getOpenBalDetails);

            $results = $all->groupBy('Staff_Number');


            $getAll = $results->map(function($item)
            {
                return $item->all();
            });

            dd($getAll);

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.