Sum result of Count, is it possible?

Asked

Viewed 40 times

0

I need to add the result of a count() in a table and the function sum() does not work for results obtained with count().

This is inside a foreach:

$occurrence->people_involved->where('tipo_de_envolvimento','VÍTIMA')->count()

The result is a number for each loop item.

I need to add this result, out of the foreach, clear. How to do?

  • 1

    I didn’t understand need to explain better

2 answers

1

Just use an accumulator variable:

$contador = 0;

foreach(...){
   $count = $occurrence->people_involved->where('tipo_de_envolvimento','VÍTIMA')->count();
   $contador = $contador + $count;
}


dd($contador);

0

I don’t know if the following answer suits you, since I’m not 100% sure I understand what you need, but you could power an array with the results of count() within your foreach() and then make the sums you wish.

Example:

$counts = array();

/*isso aqui seria seu foreach()*/

for($x=0;$x<10;$x++){ 

   /*aqui você substitui o rand() pelo seu count() e alimenta o array*/
   $counts[] = rand(0, 150);

}
/*agora é só correr o array preenchido, fazendo as somas que você quiser.*/    

I hope I helped. Anything comments there that I try to give some more hint. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.