如何在Laravel中更新主数组

时间:2019-07-18 09:00:51

标签: arrays laravel laravel-5.8

在foreach循环中更改数据的主阵列上,我需要帮助。 这是我的代码:

$query = DB::table('autostk')
    ->where('autostk.branchid', $branch_id)
    ->where('autostk.itemcode', $request->itemcode)
    ->whereDate('autostk.date', '<=', $request->tdate)
    ->where('autostk.branchid', $branch_id)
    ->leftjoin('journal', 'autostk.refno', '=', 'journal.vno')
    ->where('journal.code', '>=', 100)
    ->where('journal.branchid', $branch_id)
    ->leftjoin('accounts', 'journal.code', '=', 'accounts.code')
    ->where('accounts.branchid', $branch_id)
    ->select('journal.code', 'accounts.title', 'autostk.*')
    ->orderBY('date')->get()
    ->map(function ($item, $key) {
        return (array)$item;
    })
    ->all();

foreach ($query as $row) {
    if (is_null($row['qtyin'])) {
        $row['qtyin'] = 0;
    }
    if (is_null($row['qtyout'])) {
        $row['qtyout'] = 0;
    }
    if (is_null($row['rate'])) {
        $row['rate'] = 0;
    }
    if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
        $stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
        if ($bal > 0) {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            if ($bal > 0 && $stkval > 0) {
                $avgrate = $stkval / $bal;
            }
        } else {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            $avgrate = $row['rate'];
        }
    } else {
        $bal = $bal + $row['qtyin'] - $row['qtyout'];
    }
    $row['balqty'] = $bal;
    $row['avgrate'] = $avgrate;
}

我的问题是如何使用对$ row所做的更改来更新$ query。我是php和laravel的新手,曾经尝试过push(),put()等。在这种情况下,不知道需要哪个函数。

3 个答案:

答案 0 :(得分:0)

要保留已在foreach内应用于$row的更改,只需通过引用将其传递:

foreach ($query as &$row) { //notice the & before $row 

或者,您可以只将foreach内的代码移至map闭包内:

->map(function ($item, $key) use ($bal, $avgrate) {

    $row = (array)$item;

    if (is_null($row['qtyin'])) {
        $row['qtyin'] = 0;
    }
    if (is_null($row['qtyout'])) {
        $row['qtyout'] = 0;
    }
    if (is_null($row['rate'])) {
        $row['rate'] = 0;
    }
    if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
        $stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
        if ($bal > 0) {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            if ($bal > 0 && $stkval > 0) {
                $avgrate = $stkval / $bal;
            }
        } else {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            $avgrate = $row['rate'];
        }
    } else {
        $bal = $bal + $row['qtyin'] - $row['qtyout'];
    }
    $row['balqty'] = $bal;
    $row['avgrate'] = $avgrate;

    return $row;

}) 

答案 1 :(得分:0)

尝试以下代码:

$query = DB::table('autostk')
    ->where('autostk.branchid', $branch_id)
    ->where('autostk.itemcode', $request->itemcode)
    ->whereDate('autostk.date', '<=', $request->tdate)
    ->where('autostk.branchid', $branch_id)
    ->leftjoin('journal', 'autostk.refno', '=', 'journal.vno')
    ->where('journal.code', '>=', 100)
    ->where('journal.branchid', $branch_id)
    ->leftjoin('accounts', 'journal.code', '=', 'accounts.code')
    ->where('accounts.branchid', $branch_id)
    ->select('journal.code', 'accounts.title', 'autostk.*')
    ->orderBY('date')->get()
    ->map(function ($item, $key) {
        return (array)$item;
    })
    ->all();

$row_data = [];
foreach ($query as $row) {
    if (is_null($row['qtyin'])) {
        $row['qtyin'] = 0;
    }
    if (is_null($row['qtyout'])) {
        $row['qtyout'] = 0;
    }
    if (is_null($row['rate'])) {
        $row['rate'] = 0;
    }
    if ($row['vtype'] = 'PI' && $row['qtyin'] > 0) {
        $stkval = ($bal * $avgrate) + ($row['qtyin'] * $row['rate']);
        if ($bal > 0) {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            if ($bal > 0 && $stkval > 0) {
                $avgrate = $stkval / $bal;
            }
        } else {
            $bal = $bal + $row['qtyin'] - $row['qtyout'];
            $avgrate = $row['rate'];
        }
    } else {
        $bal = $bal + $row['qtyin'] - $row['qtyout'];
    }
    $row['balqty'] = $bal;
    $row['avgrate'] = $avgrate;
    $row_data[] = $row;
}

现在使用$row_data

答案 2 :(得分:0)

选项1

//use toArray() in last to get the result as an array
$query = ...->all()->toArray();

foreach ($query as $key => $row) {
     //inside here instead of using $row, use $query[$key]
     //so for example $row['rate'] = 0; becomes:
     $query[$key]['rate'] = 0;
}

选项2

//use toArray() in last to get the result as an array
$query = ...->all()->toArray();

//use pass by reference with help of &
foreach ($query as $key => &$row) {
   ...
}

但是,请谨慎使用按引用传递方法,否则,如果重用同一数组,则可能会遇到问题。

选项3

$query = ...->all();
foreach ($query as $key => $row) {
    //access it as object
    //instead of using $row['qtyin'] use:
    $row->qtyin = 0;
}

由经销商选择:)