connection('mongodb'); } /* ATTRIBUTES */ public function getMsgAttribute($value): string { return utf8_decode($value); } public function setMsgAttribute($value): void { $this->attributes['msg'] = utf8_encode($value); } public function getSubjectAttribute($value): string { return utf8_decode($value); } public function setSubjectAttribute($value): void { $this->attributes['subject'] = utf8_encode($value); } /* METHODS */ public static function countGroupBy(array $fields,array $where=[]): Collection { $query = collect(); if (count($where)) { $where_condition = []; foreach ($where as $key => $values) { if (! is_array($values)) throw new \Exception('Where values must be an array.'); switch ($x=Arr::get($values,0)) { case '$gt': case '$gte': $where_condition[$key] = [ $x => Arr::get($values,1)]; break; case '$in': default: $where_condition[$key] = ['$in' => $values]; } } $query->push([ '$match' => [ '$and'=> [$where_condition]] ]); } $gb = collect(); foreach ($fields as $field) if (is_array($field)) { foreach ($field as $k=>$v) { $gb->put('datetime',['$dateToString'=>['format'=>$v,'date'=>'$'.$k]]); } } else { $gb->put($field,'$'.$field); } $query->push([ '$group' => [ '_id' => $gb->toArray(), 'count' => ['$sum' => 1] ] ]); return (new self) ->groupBy($field) ->raw(function($collection) use ($query) { return $collection->aggregate( $query->toArray() ); }); } }