'Operation count different id's

i need to do this operation:

introducir la descripción de la imagen aquí

Count the total of each number of records with a different 'rrpp': 1,2,3,4 ∞

I always use this to sum the records of a RRPP:

  $variable = Modelo::where('dia_id' , $request->id)->where('rrpp' , 1)->count('id');

but this way I only get the result of 1 in specific. And what I need is to get the result like this

help, thanks



Solution 1:[1]

you only get result 1 because you use this where('dia_id' , $request->id) in your query, if you want to count rrpp=1 use this query:

$variable = Modelo::where('rrpp' , 1)->count('id');

if you want to count all rrpp use this query:

$variable = Modelo::select('rrpp', 
    DB::raw('count(*) as total'))
    ->groupBy('rrpp')
    ->get()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1