Change db to database in database.php file for connecting with mongodb.
After than clear the cache for configurations
php artisan config:clear
use MongoDB\BSON\ObjectID as MongoID;
function convert_to_mongo_date($date)
{
if($date == null || !isset($date))
return null;
$carbon_date = Carbon::createFromFormat('d/m/Y', $date);
try {
$mongo_date = new MongoDate(strtotime($carbon_date->toDateString()) * 1000);
return $mongo_date;
}
catch(Exception $e) {
Log::info('MongoDate conversion error '.$e);
return new MongoDate();
}
}
function convert_to_date_from_sec($date)
{
if($date == null || !isset($date))
return "01/01/1990";
try
{
$secs = $date->toDateTime()->format('U.u');
return date('d/m/Y', $secs);
}
catch(Exception $e)
{
return "01/01/1990";
}
}
$fromDate = new MongoDate( strtotime('-1 month' ));
$currentDate = new MongoDate( strtotime('+1 day' ));
$user_signups_raw = DB::collection('users')->raw(function($collection) use($fromDate, $currentDate) { return $collection->aggregate (
array (
array ( '$match' => array ( 'created_at' => array( '$gt' => $fromDate, '$lt' =>$currentDate) ) ), array ( '$group' => array ( '_id' => array( '$dayOfYear' => '$created_at', ), 'count' => array ( '$sum' => 1 ), 'date' => array ( '$first' => '$created_at' ) ) ), array ( '$sort' => array ( '_id' => 1 ), )
)
); });
$user_signups = [];
foreach ($user_signups_raw as $doc)
{
$item = json_encode( $doc->getArrayCopy());
$item = $doc->getArrayCopy();
array_push($user_signups, $item);
}