我正在盤算從我的資料庫中創建統計資料。我正在改變應用程式,之前的統計資料是由呼叫表創建的。但我改變了這一點,我創建了一個新的統計表,并有以下列:
id_ployed, call_id, created_at(nullable)
id, id_ployed, call_id, created_at(nullable), updated_at(nullable)
和資料例子為例:
52,60,888988,4
53,60,88989,10
54,60,88990,6
62,9,88999,1
我的控制器中有這個函式,但是回傳的資料是空的,這是不可能的:
。$estadosLlamadas = EstadoLlamada::orderBy('desc'/span>)->get();
if(isset($request-> fromDate) && isset($request-> toDate)){
$fromDate = Carbon::parse($request->get('fromDate') )。)
$toDate = Carbon::parse($request->get('toDate') )。)
$fromDate = $fromDate->format('Y-m-d') 。
$toDate = $toDate-> format('Y-m-d')。
}else{
$fromDate = new Carbon('first day of this month') 。
$toDate = new Carbon('last day of this month');
$fromDate = $fromDate->format('Y-m-d'/span>)。
$toDate = $toDate-> format('Y-m-d')。
}
$teleoperadoras = auth()-> user()-> whereIs('teleoperadora')-> activos()-> select(['id', 'nombre'])-> orderBy('nombre', 'desc')-> get();
$array = [
'toDate' => $toDate,
'fromDate' => $fromDate,
'nombresEstados' => $estadosLlamadas->pluck('desc')-> toArray()。
'coloresEstados' => $estadosLlamadas-> pluck('hex')->toArray()
];
$query = Estadisticas::query()
->whereDate('estadisticas.created_at', '<=', $toDate)
->whereDate('estadisticas.created_at', '>=', $fromDate)
->whereIn('estadisticas.id_empleado', $teleoperadoras->pluck('id')
->加入(' users', 'estadisticas. id_empleado', '=', 'users.id')->最新('estadisticas.uped_at')->get();
foreach($teleoperadoras as$teleoperadora) {
$array['teleoperadoras'][] = $teleoperadora->nombre。
$array['id_teleoperadoras'] [] = $teleoperadora->id。
$array[$teleoperadora->id]['resultados'] = [] 。
$array['llamadas'][] = $query-> where('id_empleado', $teleoperadora->id)-> count()。
$array['llamadasTodo'/span>][$teleoperadora-> id] = $query->where('id_empleado', $teleoperadora-> id);
foreach($estadosLlamadas as$estado) {
$array[$teleoperadora->id]['resultados'] [] = $query-> where('id_empleado', $teleoperadora-> id)
->where('id_estado', $estado->id)-> count()。
}
}
$array['nllamadas'] = $query->count();
$array['fromDate'] = $fromDate . " 00:00:00";
$array['toDate'] = $toDate 。" 23:59:59"。
$roleUser = auth()-> user()-> getRoles()-> first();
return [
'datos' => $array, 'estados' => $estadosLlamadas,
'teleoperadoras' => $teleoperadoras, 'roleUser' => $roleUser,
];
這個用chartJS填充靜態:
這個用chartJS填充靜態。
$.ajax({
type: 'GET',
url: "{{ route('admin.llamadas.estadisticas') }}"。
data: { 'fromDate'/span>: fromDate, 'toDate'/span>: toDate },
success: function(response){
console.log( response)。
$("#loadMe").modal('hide') 。
var totalCall = 0;
let totalCallByOperator = response["datos"]["llamadas"] 。
$("#totalCall").append(response.datos.nllamadas)
//append all teleoperator in view with calls number。
response["datos"]["teleoperadoras"].forEach(function(teleoperadora, index){
$("#teleoperadoras").append('<b class="teleoperadora"> ' teleoperadora " 。" totalCallByOperator[index] '</b> <br> ')。
});
//產生一般的統計資料。
var ctx = document. getElementById('canvas1').getContext('2d') 。
var myChart = new Chart(ctx, {
type: 'bar'。
data: {
標簽: 回應["datos"]["teleoperadoras"] 。
datasets: [{
label: 'Total de llamadas por operadora',
data: totalCallByOperator,
backgroundColor: [
'rgba(255, 99, 132, 0.2)'/span>,
'rgba(255, 159, 64, 0.2)',
'rgba(255, 205, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(201, 203, 207, 0.2)'.
],
borderColor: [
'rgb(255, 99, 132)'。
'rgb(255, 159, 64)'。
'rgb(255, 205, 86)'。
'rgb(75, 192, 192)'。
'rgb(54, 162, 235)'。
'rgb(153, 102, 255)'。
'rgb(201, 203, 207)'.
],
borderWidth: 1。
}]
},
options: {
scales: {
y: {
beginAtZero: true: beginAtZero: true
}
}
}// end options//結束圖表物件。
},
error: function(xhr){
alert(xhr.responseText) 。
}
});
而我的控制器的回應是:
{datos: {9: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]},... }, estados: [,...],...}, ...}.
資料: {9: {結果。 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}, ... }
9: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
11: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
22: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
23: {resultados: [0, 0, 0, 0, 0。0, 0, 0, 0, 0] }
24: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0] }
25: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
26: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
49: {resultados: [0, 0, 0, 0, 0。0, 0, 0, 0, 0] }
50: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }
60: {resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0] }
coloresEstados: ["#d26a5c"/span>, "#f3b760"/span>, "#46c37b"/span>, "#343a40"/span>, "#6c757d"/span>, "#d26a5c", "#d26a5c", "#5c80d1", "#70b9eb", ... ]
fromDate: "2021-08-01 00:00:00"
id_teleoperadoras: [9, 23, 24, 60, 22, 49, 11, 50, 25, 26]
llamadas: [0, 0, 0, 0, 0。0, 0, 0, 0]
llamadasTodo: {9: [], 11: [], 22: [], 23: [], 24: [], 25: [], 26: [], 49: [], 50: [], 60: [] }
nllamadas: 682 [].
nombresEstados: ["ANULADA", "AUSENTE", "CONFIRMADA"。"CONFIRMADA/ANULADA", "CONFIRMADA/AUSENTE", "ERRONEO NO EXISTE",... ]
teleoperadoras: ["ARANZAZU SOLIS SANCHEZ", "CRISTINA LOPEZ UBRIC", "CRISTINA MORALES FERNANDEZ",..._/span>]
toDate: "2021-08-31 23:59:59"
estados: [,...]
roleUser: "admin"
teleoperadoras: [{id: 9, nombre: "ARANZAZU SOLIS SANCHEZ"}, {id: 23, nombre: "CRISTINA LOPEZ UBRIC"}, ...]
陣列 "resultados "應該是我統計的資料的地方。這個函式以前是好的,但現在不作業了。
我的模型是:
class Estadisticas extends Model
{
protected $table = 'estadisticas';
protected $primarykey = 'id';
public $timestamps = false;
protected $fillable = [
'id_empleado', 'id_llamada', 'id_estado', 'created_at', 'uped_at'public function teleoperadora()
{
return $this-> hasOne('AppUser', 'id', 'id_empleado')->withDefault()。
}
public function estado()
{
return $this->hasOne('AppEstadoLlamada', 'id', 'id_estado') 。
}
}
我希望有人能幫助我,謝謝你的閱讀,對我的英語感到抱歉
uj5u.com熱心網友回復:我的問題是:
Estadisticas::whereBetween('created_at', [$fromDate, $toDate] )
->where('id_empleado'/span>, $teleoperadora)
->with('teleoperadora')->get()。
whereBetween
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320612.html
標籤:
