Start on user dashboard

This commit is contained in:
Deon George
2021-10-26 23:19:55 +11:00
parent 8c127ba5da
commit a0db589dc5
8 changed files with 309 additions and 7 deletions

View File

@@ -5,6 +5,8 @@
*/
namespace App\Traits;
use Illuminate\Database\Eloquent\Collection;
trait UseMongo
{
/**
@@ -40,4 +42,41 @@ trait UseMongo
{
$this->attributes['subject'] = utf8_encode($value);
}
/* METHODS */
public static function countGroupBy(string $field,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.');
$where_condition[$key] = ['$in' => $values];
}
$query->push([
'$match' => $where_condition
]);
}
$query->push([
'$group' => [
'_id' => [$field=>'$'.$field],
'count' => ['$sum' => 1]
]
]);
return (new self)
->groupBy($field)
->raw(function($collection) use ($query) {
return $collection->aggregate(
$query->toArray()
);
});
}
}