3 months ago

Find and replace in Laravel collection

Posted 3 months ago by Zaheer

I am trying to update a laravel collection with another collection. Here are my collections :

$acc = DB::table('accounts')
    ->where('accounts.branchid', $branch_id)
$rslt = DB::table('journal')
    ->where('journal.branchid', $branch_id)
    ->where(function($q) {$q->where('journal.cancel','!=',1)->orWhereNull('journal.cancel');})
    ->select('code',DB::raw('sum(IFNULL(dr,0) - IFNULL(cr,0)) as total'))->groupBy('code')->Get('code','total');

What I want is to update $acc->clbal with $rslt->total where $acc->code = $rslt->code I tried this but it does not work

foreach ($rslt as $row) {
    $getacc = $acc->where('code',$row->code);
    $getacc->clbal = $row->total;

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