在Laravel Collection中查找和替换

时间:2019-07-11 08:10:33

标签: laravel collections laravel-5.8

我正在尝试使用另一个集合更新laravel集合。这是我的收藏集:

ALTER DEFAULT PRIVILEGES
FOR USER postgres
IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO postgres;

我想要的是用$ rslt-> total更新$ acc-> clbal,其中$ acc-> code = $ rslt-> code

我尝试了这个,但是没有用

ERROR:  must be member of role "postgres"
SQL state: 42501

1 个答案:

答案 0 :(得分:0)

这是解决方案。

$acc = DB::table('accounts')
    ->where('accounts.branchid', $branch_id)
    ->select('code','title','opbal','clbal')->get();
$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');

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