Eloquence\Behaviours\Cacheable::rebuildCacheRecord PHP Method

rebuildCacheRecord() public method

Rebuilds the cache for the records in question.
public rebuildCacheRecord ( array $config, Model $model, $command, null $aggregateField = null ) : mixed
$config array
$model Illuminate\Database\Eloquent\Model
$command
$aggregateField null
return mixed
    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]);
    }