List of things needed to upgrade to the latest laravel
- Change route files (Now it's included in routes folder -> web)
- use Jenssegers\Mongodb\Eloquent\Model as Eloquent; //Use MongoDB as a model for every Model
- Use latest Jssenger laravel-mongodb plugins
- Use PHP > 5.7
- Install PHP driver from http://pecl.php.net/package/mongodb/1.2.8/windows and also in the php.ini config (If you're using xampp for windows, you likely have to use 32bit version of php driver)
If you're upgrading from previous, use the following command to load the config file again
composer dump-autoload
- Change route files (Now it's included in routes folder -> web)
- use Jenssegers\Mongodb\Eloquent\Model as Eloquent; //Use MongoDB as a model for every Model
- Use latest Jssenger laravel-mongodb plugins
- Use PHP > 5.7
- Install PHP driver from http://pecl.php.net/package/mongodb/1.2.8/windows and also in the php.ini config (If you're using xampp for windows, you likely have to use 32bit version of php driver)
If you're upgrading from previous, use the following command to load the config file again
composer dump-autoload
MongoID class is not available now and have to use the ObjectID Class
use MongoDB\BSON\ObjectID as MongoID;
MongoDate class is not available now and have to use the UTCTime Class
The previous MongoDate is now used as UTCDateTime and hence little conversion is needed to store the right MongoDate now. 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";
}
}
strtotime function changed to the following for filter dates
$fromDate = new MongoDate( strtotime('-1 month' ));
$currentDate = new MongoDate( strtotime('+1 day' ));
You need to put extra array while doing aggregations
$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 ), )
)
); });
And aggregate results is now with collections and you need the following snippets to convert it to usable format
$user_signups = [];
foreach ($user_signups_raw as $doc)
{
$item = json_encode( $doc->getArrayCopy());
$item = $doc->getArrayCopy();
array_push($user_signups, $item);
}