I am trying to configure MongoDB to work with my Laravel 5.1 Homestead instance on a virtual Ubuntu 14.04 machine. I was able to successfully install the latest version of MongoDB which supports PHP 7.0 using sudo pecl install mongodb
(this is correct for 7.0, not sudo pecl install mongo
anymore).
I then added the extension in my php.ini files (all three) on my Ubuntu machine, each in:
/etc/php/7.0/cli/php.ini
/etc/php/7.0/fpm/php.ini
/etc/php/7.0/cgi/php.ini
This is the extension I wrote which is correct for use with PHP 7.0:
extension=mongodb.so
(not mongo.so anymore)
When I run phpinfo()
in my browser, it states that MongoDB is properly configured with my PHP 7.0.
If MongoDB is properly configured, how come I keep getting:
Fatal error: Class 'MongoDate' not found
when I try to run my migrations and seeds with php artisan migrate:refresh --seed
?
I already tried:
- rebooting the Ubuntu machine with
vagrant reload
andvagrant reload --provision
- Restarting PHP and Nginx with
sudo service nginx restart
andsudo service php7.0-fpm restart
Neither have worked.
Throughout our app we were regularly converting unix timestamps into MongoDate instances. Example:
I've therefore created a class to allow conversion between unix timestamps and the new
MongoDB\BSON\UTCDateTime
, and backExample usage:
As you mentioned you're using the new Mongo extension for PHP 7.
The class names have changed from the older version, i.e.
MongoClient
is nowMongoDB\Driver\Manager
MongoDate
is nowMongoDB\BSON\UTCDateTime
I'm not sure how backwards compatible everything is, but this should get you started!