Version 5.0 of the official Laravel MongoDB integration is now available. This major update introduces breaking changes to better align the MongoDB integration with Laravel’s conventions and behaviors, simplifying MongoDB usage for developers already familiar with Eloquent.
What’s New
- ID aliasing: Automatic aliasing of
idto_idin MongoDB is now supported, eliminating the need for specifying protected$primaryKey = '_id'in model classes, simplifying the code. Replace$model->_idwith$model->idin your code. - Date handling enhancements: All
DateTimeInterfaceobjects, including Carbon, are now automatically converted to MongoDB’sUTCDateTimefor insert and update operations. Similarly, when retrieving data, MongoDB’sUTCDateTimeis automatically converted back into Carbon date with the default timezone. This change simplifies working with dates in MongoDB, reducing the need for custom Eloquent casts. - Results as objects: Query results from MongoDB will now be returned as
stdClassobjects instead of arrays. The object properties can now be accessed using$item->namerather than$item['name']. - Simplified model customization:
$collectionhas been replaced by$tablefor customizing the collection name in Eloquent models, aligning with Laravel convention. - Provider removals: Several MongoDB-specific service providers have been removed, including
Auth\PasswordResetServiceProvider,MongoDBQueueServiceProvider, andQueue\Failed\MongoFailedJobProvider. These providers are no longer necessary, asMongoDB\Connectionis now fully compatible with Laravel’s classes. - Connection and logging updates:
MongoDB\Laravel\Connectionhas been replaced withMongoDB\Connection, and the query logging now uses MongoDB Monitoring Events.
Refer to the upgrade guide to handle the breaking changes.