public function rebuildCacheRecord(array $config, Model $model, $command, $aggregateField = null)
{
$config = $this->processConfig($config);
$modelTable = $this->getModelTable($model);
if (is_null($aggregateField)) {
$aggregateField = $config['foreignKey'];
} else {
$aggregateField = snake_case($aggregateField);
}
$sql = DB::table($modelTable)->select($config['foreignKey'])->groupBy($config['foreignKey']);
if (strtolower($command) == 'count') {
$aggregate = $sql->count($aggregateField);
} else {
if (strtolower($command) == 'sum') {
$aggregate = $sql->sum($aggregateField);
} else {
if (strtolower($command) == 'avg') {
$aggregate = $sql->avg($aggregateField);
} else {
$aggregate = null;
}
}
}
return DB::table($config['table'])->update([$config['field'] => $aggregate]);
}