0

I have a Foreach like the following to display data from the database:

$post = $request->json()->all();

$result = [];
$stock = BrandOutletStock::with('brandItem.brandCategory')->where('id_brand_outlet', $post['id_outlet'])->get()->toArray();

foreach ($stock as $element) {

    $element['brand_item']['qty'] = $element['qty'];
    $qty = $element['qty'];
    $element['brand_item']['opname_pagi'] = 0;
    $element['brand_item']['opname_pagi_after'] =  0;

    $element['brand_item']['opname_delivery'] = 0;
    $element['brand_item']['opname_delivery_after'] =  0;

    $element['brand_item']['opname_malam'] = 0;
    $element['brand_item']['opname_malam_after'] =  0;
    $cat = $element['brand_item']['brand_category']['category'];

    unset($element['brand_item']['brand_category']);


    $select2 = BrandOutletDelivery::with(['brandOutletDeliveryItems' => function($q) use($element, $post){
        $q->where('id_brand_item', $element['id_brand_item'])->whereDate('created_at', $post['date']);
    }])->whereDate('created_at', $post['date'])->where('id_brand_outlet', $post['id_outlet']);

    $select2 = $select2->orderBy('created_at','desc')->get();



    foreach ($select2 as $key => $value){
        foreach ($value['brandOutletDeliveryItems'] as $key1 => $val) {
            $value['id_brand_opname'] = $val['id_brand_delivery'];
            $value['id_brand_item'] = $val['id_brand_item'];
            $value['id_unit'] = $val['id_unit'];
            $value['qty_before'] = $val['qty_before'];
            $value['qty_change'] = $val['qty_change'];
            $value['qty_after'] = $val['qty_after'];
        }
           unset($select2[$key]['brandOutletDeliveryItems']);

    }
    $time2 = (count($select2) != 0) ? strtotime($value['created_at']) :  0;

    $select3 = $select2->toArray();

    if (!empty($select3)) {

        $column2      = array_column($select3, 'type');
        $key_pagi     = array_search('Delivery In', $column2);
        $key_delivery = array_search('Delivery Out', $column2);

        // return $select[$key_malam]['qty_before'];


    $element['brand_item']['opname_delivery']       = ($select3[$key_delivery]['qty_change'] ?? 0 > 0) ? "-".$select3[$key_delivery]['qty_change'] ?? 0 : $select3[$key_delivery]['qty_change'] ?? 0; //Output from sum
}

If I put the code outside the foreach then it has output like this

[
    {
        "id": 125,
        "id_brand": 1,
        "id_brand_outlet": 15,
        "id_user": 5,
        "id_brand_outlet_tujuan": null,
        "alasan": "gabut",
        "no_surat": "QWRR",
        "created_at": "2020-02-21 14:23:38",
        "updated_at": "2020-02-21 14:23:38",
        "type": "Delivery In",
        "id_brand_opname": 125,
        "id_brand_item": 2,
        "id_unit": 2,
        "qty_before": 10,
        "qty_change": 1,
        "qty_after": 11
    },

    {
        "id": 123,
        "id_brand": 1,
        "id_brand_outlet": 15,
        "id_user": 5,
        "id_brand_outlet_tujuan": 14,
        "alasan": "gabut",
        "no_surat": "QWE11",
        "created_at": "2020-02-21 14:23:09",
        "updated_at": "2020-02-21 14:23:09",
        "type": "Delivery Out",
        "id_brand_opname": 123,
        "id_brand_item": 2,
        "id_unit": 2,
        "qty_before": 10,
        "qty_change": 1,
        "qty_after": 9
    },
    {
        "id": 122,
        "id_brand": 1,
        "id_brand_outlet": 15,
        "id_user": 5,
        "id_brand_outlet_tujuan": 14,
        "alasan": "gabut",
        "no_surat": "QWE12",
        "created_at": "2020-02-21 14:22:45",
        "updated_at": "2020-02-21 14:22:45",
        "type": "Delivery Out",
        "id_brand_opname": 122,
        "id_brand_item": 2,
        "id_unit": 2,
        "qty_before": 11,
        "qty_change": 1,
        "qty_after": 10
    }

]

How do I add the elements of "QTY_CHANGE" from the Array? Example : In the Array Output, "QTY_Change" each has a Value of 1, and there are three objects, if added together the result is 3. Thanks

1 Answer 1

1

You can do it like this:

 $sum = 0; // create a variable
 foreach ($select2 as $key => $value){
                foreach ($value['brandOutletDeliveryItems'] as $key1 => $val) {
                   // your code
                    $sum+= $val['qty_change'] ; //add this line
                }

 }

echo $sum; // you will get the sum here
Sign up to request clarification or add additional context in comments.

2 Comments

$sum+= $val['qty_change'] ; Was it placed above or below?
You can place it anywhere inside inner loop i.e foreach ($value['brandOutletDeliveryItems'] as $key1 => $val) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.