1

i have the next collection:

enter image description here

It is grouped by zone and fundo and I need to sum all the sup_ha that each fundo contains

$transporFundo = $tansporCollect->groupBy(['zona','fundo']);
        
foreach ($transporFundo as $fundo) {
    $supFundo = $fundo->sum('sup_ha');
    dd($supFundo);
}

that is to say that for example of my collection for zona 1 and fundo 805 I would have to add the sup_ha field of the 364 records that are seen.

I tried to do it but I don't know how to access the field, as seen in my code I tried and it returns 0.

I hope you can help me, thanks

**

UPDATE

** ok, remove the group by, for example show a collection like the following, the original has 700k of records

Illuminate\Support\Collection {#778646 ▼
  #items: array:4 [▼
    0 => array:3 [▼
      "zona" => 1
      "sup_ha" => 20
      "fundo" => 805
    ]
    1 => array:3 [▼
      "zona" => 1
      "sup_ha" => 10
      "fundo" => 805

    ]
    2 => array:3 [▼
      "zona" => 2
      "sup_ha" => 5
      "fundo" => 800
    ]
    3 => array:3 [▼
      "zona" => 2
      "sup_ha" => 10
      "fundo" => 900
    ]
  ]
}

so, I need the sum of the sup_ha of equal fundo, like this:

fundo | sum
805   | 30
800   | 5
900   | 10

And I need the sum of the sup_ha of equal zona, like this:

zona  | sum
1   | 30
2   | 15

Given this, that's why I had made a group by to calculate the sum of the fundo's. so now I don't know whether to think if it was right to do it like this

I hope you have clarified

Ersoy
  • 8,816
  • 6
  • 34
  • 48
Pamela Rojas
  • 67
  • 3
  • 10
  • According to this [question](https://stackoverflow.com/questions/27803573/laravel-sum-eloquent-collection) it could be done like this: `$supFundo = $fundo->sum(function($fun){return $fun->sum('sup_ha');});` – user3647971 Jul 04 '20 at 20:03
  • Does this answer your question? [Laravel sum eloquent collection](https://stackoverflow.com/questions/27803573/laravel-sum-eloquent-collection) – user3647971 Jul 04 '20 at 20:07
  • the first returns 0 to. the second don't work for me because he is working on a related collection. im try supFundo = $fund->zona->fundo->sum('sup_ha'); and supFundo = $fund->fundo->sum('sup_ha'); and I get an error that the variable zona/ fundo is not in the collection instance – Pamela Rojas Jul 04 '20 at 21:10
  • Your collection is nested like this: array(array(array('zona','destino'...))); You should loop through all of those. `foreach($transporFundo as $zona){foreach($zona as $fundo){$sumFundo = $fundo->sum('sup_ha');}}}` – user3647971 Jul 04 '20 at 21:27
  • Please share Minimal and Reproducible Example. You have a screenshot and it is hard to help. Share the `json`/`array` version as input and what do you expect as output. https://stackoverflow.com/help/minimal-reproducible-example – Ersoy Jul 04 '20 at 21:28
  • 1
    I put an update clarifying the problem – Pamela Rojas Jul 04 '20 at 22:15

2 Answers2

0

Assuming your collection is assigned to $collection.

so, I need the sum of the sup_ha of equal fundo.

  • High order functions
return $collection->groupBy('fundo')->map->sum('sup_ha');
  • Alternative
return $collection
    ->groupBy('fundo')
    ->transform(function (Collection $sub) {
        return $sub->sum('sup_ha');
    });
  • It prints
{
    "805": 30,
    "800": 5,
    "900": 10
}

And I need the sum of the sup_ha of equal zona

  • High order functions
return $collection->groupBy('zona')->map->sum('sup_ha');
  • Alternative
return $collection
    ->groupBy('zona')
    ->transform(function (Collection $sub) {
        return $sub->sum('sup_ha');
    });

It prints

{
    "1": 30,
    "2": 15
}
Ersoy
  • 8,816
  • 6
  • 34
  • 48
0

Assuming your collection is assigned as $collection

    $zonaArray = [];
    $fundoArray = [];
    $sum = 0;
    foreach ($collection as $key => $t) {
        if (!in_array($t['zona'], $zonaArray)) {
            $zonaArray[$key]['zona'] = $t['zona'];
            $zonaArray[$key]['sum'] = (float) $t['sup_ha'];
        } else {
            $zonaArray[$key]['sum'] = (float) $t['sup_ha'];
        }

        if (!in_array($t['fundo'], $fundoArray)) {
            $fundoArray[$key]['fundo'] = $t['fundo'];
            $fundoArray[$key]['sum'] = (float) $t['sup_ha'];
        } else {
            $fundoArray[$key]['sum'] = (float) $t['sup_ha'];
        }
    }

    $checkzona = [];
    $value = 0;
    $finalZone = [];
    $i = 0;

    foreach ($zonaArray as $key => $val) {

        if (in_array($val['zona'], $checkzona)) {
            unset($finalZone[$i - 1]);
            $val['sum'] = $value + (float) $val['sum'];
            $finalZone[$i] = $val;
            $i++;
        } else {
            $checkzona[] = $val['zona'];
            $value = $val['sum'];
            $finalZone[$i] = $val;
            $i++;
        }
    }

Your zone wise sum output is looking like below.

    array:2 [▼
     1 => array:2 [▼
      "zona" => 1
      "sum" => 30.0
    ]
    3 => array:2 [▼
     "zona" => 2
     "sum" => 15.0
    ]
 ]

For fundo sum:

    $checkfundo = [];
    $valuefundo = 0;
    $finalFundo = [];
    $k = 0;

    foreach ($fundoArray as $key => $fundo) {
        if (in_array($fundo['fundo'], $checkfundo)) {
            unset($finalFundo[$k - 1]);
            $fundo['sum'] = $valuefundo + (float) $fundo['sum'];
            $finalFundo[$k] = $fundo;
            $k++;
        } else {
            $checkfundo[] = $fundo['fundo'];
            $valuefundo = $fundo['sum'];
            $finalFundo[$k] = $fundo;
            $k++;
        }
    }

Your fundo wise sum output is looking like below.

   array:3 [▼
    1 => array:2 [▼
      "fundo" => 805
      "sum" => 30.0
   ]
   2 => array:2 [▼
     "fundo" => 800
     "sum" => 5.0
   ]
   3 => array:2 [▼
      "fundo" => 900
     "sum" => 10.0
   ]
 ]

I hope it's working for you.