checkLedger(); } else { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); $this->checkLedger(); } } nlog("Finished checking company ledgers"); } public function checkLedger() { nlog("Checking ledgers...."); CompanyLedger::where('balance', 0)->where('adjustment', '!=', 0)->cursor()->each(function ($company_ledger){ if($company_ledger->balance > 0) return; $last_record = CompanyLedger::where('client_id', $company_ledger->client_id) ->where('company_id', $company_ledger->company_id) ->where('balance', '!=', 0) ->orderBy('id', 'DESC') ->first(); if(!$last_record) return; nlog("Updating Balance NOW"); $company_ledger->balance = $last_record->balance + $company_ledger->adjustment; $company_ledger->save(); }); } }