Laravel MongoDB releases version 5.0
Published on by Eric L. Barnes
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
id
to_id
in MongoDB is now supported, eliminating the need for specifying protected$primaryKey = '_id'
in model classes, simplifying the code. Replace$model->_id
with$model->id
in your code. -
Date handling enhancements: All
DateTimeInterface
objects, including Carbon, are now automatically converted to MongoDB’sUTCDateTime
for insert and update operations. Similarly, when retrieving data, MongoDB’sUTCDateTime
is 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
stdClass
objects instead of arrays. The object properties can now be accessed using$item->name
rather than$item['name']
. -
Simplified model customization:
$collection
has been replaced by$table
for 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\Connection
is now fully compatible with Laravel’s classes. -
Connection and logging updates:
MongoDB\Laravel\Connection
has been replaced withMongoDB\Connection
, and the query logging now uses MongoDB Monitoring Events.
Refer to the upgrade guide to handle the breaking changes.
Resources
Eric is the creator of Laravel News and has been covering Laravel since 2012.