Laravel how to merge 2 collections into one [duplicate] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, August 18, 2022

Laravel how to merge 2 collections into one [duplicate]

I have this query:

$sum1 = DB::table('invoices')

        ->leftJoin('coletes','coletes.invoice_id','=','invoices.id')      
        ->selectRaw('
                sum(coletes.totallei) as totallei,
              sum(coletes.totaleuro) as total     
                ')
        ->orderBy('invoices.id')
        ->get('total');
 $sum2 =  DB::table('invoices')
->leftJoin('incasaris','incasaris.i_plati_id','=','invoices.id')
->selectRaw('
  sum(incasaris.i_totaleuro) as totalincasat,
  sum(incasaris.i_totallei) as totalincasatlei  
   ')
   ->orderBy('invoices.id')
   ->get('totalincasat');

 
 $sum1 = $sum1->merge($sum2);

This query returns enter image description here

But I would want it to be as:

 #items: array:1 [▼
    0 => {#1463 ▼
      +"totallei": "1965.2800"
      +"total": "400.00"
  +"totalincasat": "400.00"
      +"totalincasatlei": "0.00"
    }   
  ]

So only in one single array Any suggestions?

I have tried more methods such as concat but it still doesn't return a single collection..




source https://stackoverflow.com/questions/73393087/laravel-how-to-merge-2-collections-into-one

No comments:

Post a Comment