0
How do I count multiple records with Laravel? Example: Table homicios, count the columns month, date, city separately and generate the chart in Chart.
Chart 1: Jan - X quantity Feb - X quantity Etc.
Chart 2: City A - X quantity City Z - X quantity Etc.
Chart 3 Period from A to B - X quantity
The function in the controller:
public function index()
{
$total= Homicidio::whereYear('data', date('Y'))
->count();
$janeiro = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','01')
->count();
$fevereiro = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','02')
->count();
$marco = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','03')
->count();
$abril = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','04')
->count();
$maio = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','05')
->count();
$junho = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','06')
->count();
$julho = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','07')
->count();
$agosto = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','08')
->count();
$setembro = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','09')
->count();
$outubro = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','10')
->count();
$novembro = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','11')
->count();
$dezembro = Homicidio::whereYear('data', date('Y'))
->whereMonth('data','12')
->count();
$loanda = Homicidio::where('cidade_id', '=', '1')
->whereYear('data',date('Y'))
->count();
$santa_isabel = Homicidio::where('cidade_id', '=', '2')
->whereYear('data',date('Y'))
->count();
$planaltina = Homicidio::where('cidade_id', '=', '3')
->whereYear('data',date('Y'))
->count();
$santa_monica = Homicidio::where('cidade_id', '=', '4')
->whereYear('data',date('Y'))
->count();
$santa_cruz = Homicidio::where('cidade_id', '=', '5')
->whereYear('data',date('Y'))
->count();
$querencia = Homicidio::where('cidade_id', '=', '6')
->whereYear('data',date('Y'))
->count();
$sao_pedro = Homicidio::where('cidade_id', '=', '7')
->whereYear('data',date('Y'))
->count();
$porto_rico = Homicidio::where('cidade_id', '=', '8')
->whereYear('data',date('Y'))
->count();
$nova_londrina = Homicidio::where('cidade_id', '=', '9')
->whereYear('data',date('Y'))
->count();
$marilena = Homicidio::where('cidade_id', '=', '10')
->whereYear('data',date('Y'))
->count();
$itauna = Homicidio::where('cidade_id', '=', '11')
->whereYear('data',date('Y'))
->count();
$diamante_norte = Homicidio::where('cidade_id', '=', '12')
->whereYear('data',date('Y'))
->count();
return view('home', compact('janeiro',
'fevereiro',
'marco',
'abril',
'maio',
'junho',
'julho',
'julho',
'agosto',
'setembro',
'outubro',
'novembro',
'dezembro',
'total',
'loanda',
'santa_isabel',
'planaltina',
'santa_monica',
'santa_cruz',
'querencia',
'sao_pedro',
'porto_rico',
'nova_londrina',
'marilena',
'itauna',
'diamante_norte'
));
}
The view:
<div class="col-md-6 col-xs-12 form-group">
<!-- small box homicidios -->
<div class="small-box bg-red">
<div class="inner">
<h3>{!! $total !!}</h3>
<p>Homicídios em {!! date('Y') !!}</p>
</div>
<div class="icon">
<i class="ion ion-pie-graph"></i>
</div>
<a href="#" id="link_homicidios" class="small-box-footer">
Mais Informações <i class="fa fa-arrow-circle-right"></i>
</a>
</div>
<!-- grafico homicidios -->
<canvas id="grafico_homicidios" style="display:none;"></canvas>
</div>
<div class="col-md-6 col-xs-12 form-group">
<!-- small box homicidios -->
<div class="small-box bg-red">
<div class="inner">
<h3>3ª CIPM</h3>
<p>Homicídios por Cidades</p>
</div>
<div class="icon">
<i class="ion ion-pie-graph"></i>
</div>
<a href="#" id="link_cidades" class="small-box-footer">
Mais Informações <i class="fa fa-arrow-circle-right"></i>
</a>
</div>
<!-- grafico homicidios -->
<canvas id="grafico_cidades" style="display:none;"></canvas>
</div>
@section('javascript')
<script>
//exibir graficos
$( "#link_homicidios" ).click(function() {
$( "#grafico_homicidios" ).toggle( "fast" );
});
$( "#link_cidades" ).click(function() {
$( "#grafico_cidades" ).toggle( "fast" );
});
//Grafico Homicidios
var ctx = document.getElementById("grafico_homicidios");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [
"Jan", "Fev", "Mar", "Abr", "Mai", "Jun",
"Jul", "Ago", "Set", "Out", "Nov", "Dez"
],
datasets: [{
label: 'Homicídios',
data: [
{{ $janeiro }},
{{ $fevereiro }},
{{ $marco }},
{{ $abril }},
{{ $maio }},
{{ $junho }},
{{ $julho }},
{{ $agosto }},
{{ $setembro }},
{{ $outubro }},
{{ $novembro }},
{{ $dezembro }}
],
backgroundColor:
'rgba(255, 99, 132, 0.2)'
,
borderColor:
'rgba(255,99,132,1)'
,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
//Final Grafico Homicidios
//Grafico Homicidios Cidades
var ctx = document.getElementById("grafico_cidades");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [
"Diamante do Norte", "Itaúna do Sul", "Loanda", "Marilena", "Nova Londrina", "Planaltina do Paraná",
"Porto Rico", "Querência do Norte", "St. Cruz M.C.", "St. Isabel I.", "St. Mônica", "São Pedro"
],
datasets: [{
label: 'Homicídios',
data: [
{{ $diamante_norte }},
{{ $itauna }},
{{ $loanda }},
{{ $marilena }},
{{ $nova_londrina }},
{{ $planaltina }},
{{ $porto_rico }},
{{ $querencia }},
{{ $santa_cruz }},
{{ $santa_isabel }},
{{ $santa_monica }},
{{ $sao_pedro }}
],
backgroundColor:
'rgba(255, 99, 132, 0.2)'
,
borderColor:
'rgba(255,99,132,1)'
,
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
//Final Grafico Homicidios
</script>
@endsection
Did you make any code? If you have any SQL that represents this? how are you developing your system?
– novic
I managed to do it in gambiarra mode, I was giant function. I will play as answer below and see if you can improve the code please.
– Rafael Meirim
I edited the answer with the controller and the view, I think I have a notion
– Rafael Meirim
Well, I don’t see improvements in your code, maybe an optimization with just an SQL and then in memory separate the data ... maybe!
– novic
Yes, the doubt would be how to improve sql with fewer queries memso
– Rafael Meirim
Could you do a Groupby with Where per year? What do you think!
– novic