使用空结果查询 laravel eloquent
Posted
技术标签:
【中文标题】使用空结果查询 laravel eloquent【英文标题】:Query with empty results laravel eloquent 【发布时间】:2021-10-26 12:57:43 【问题描述】:我正在准备从我的数据库中创建统计数据。我正在更改应用程序,并且在从调用表创建统计信息之前。但我改变了这一点,我创建了一个新的表统计信息并拥有这一列:
id、id_ployed、call_id、created_at(nullable)、updated_at(nullable)
和数据示例例如:
52,60,88988,4
53,60,88989,10
54,60,88990,6
62,9,88999,1
我的控制器中有这个功能,但返回空数据,这是不可能的:
$estadosLlamadas = EstadoLlamada::orderBy('desc')->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');
$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'))
->join('users', 'estadisticas.id_empleado', '=', 'users.id')->latest('estadisticas.updated_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'][$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填充静态:
$.ajax(
type: 'GET',
url: " route('admin.llamadas.estadisticas') ",
data: 'fromDate': fromDate, 'toDate': 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>');
);
// generate general statistics
var ctx = document.getElementById('canvas1').getContext('2d');
var myChart = new Chart(ctx,
type: 'bar',
data:
labels: response["datos"]["teleoperadoras"],
datasets: [
label: 'Total de llamadas por operadora',
data: totalCallByOperator,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'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
// end options
); // end chart object
,
error: function(xhr)
alert(xhr.responseText);
);
我的控制器的响应是:
datos: 9: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],…, estados: [,…],…
datos: 9: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],…
9: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
11: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
22: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
23: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
24: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
25: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
26: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
49: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
50: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
60: resultados: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
coloresEstados: ["#d26a5c", "#f3b760", "#46c37b", "#343a40", "#6c757d", "#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, 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",…]
toDate: "2021-08-31 23:59:59"
estados: [,…]
roleUser: "admin"
teleoperadoras: [id: 9, nombre: "ARANZAZU SOLIS SANCHEZ", id: 23, nombre: "CRISTINA LOPEZ UBRIC",…]
array "resultados" 它应该是我的统计数据的位置。这个功能以前可以,现在不行了。
我的模型:
class Estadisticas extends Model
protected $table = 'estadisticas';
protected $primarykey = 'id';
public $timestamps = false;
protected $fillable = [
'id_empleado', 'id_llamada', 'id_estado', 'created_at', 'updated_at'
];
public function teleoperadora()
return $this->hasOne('App\User', 'id', 'id_empleado')->withDefault();
public function estado()
return $this->hasOne('App\EstadoLlamada', 'id', 'id_estado');
我希望任何人都可以帮助我。感谢阅读,对不起我的英语
【问题讨论】:
【参考方案1】:我解决了我的问题:
Estadisticas::whereBetween('created_at', [$fromDate, $toDate])
->where('id_empleado', $teleoperadora)
->with('teleoperadora')->get();
在哪里
【讨论】:
以上是关于使用空结果查询 laravel eloquent的主要内容,如果未能解决你的问题,请参考以下文章