-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Bug Report: Inability to Disable Top-Level _id
Aliasing in Laravel MongoDB Package
Description:
The mongodb/laravel-mongodb
package currently provides a configuration option rename_embedded_id_field
to disable automatic renaming of _id
fields only for embedded documents. However, there is no corresponding option to disable top-level _id
aliasing. As a result:
- Models with a custom
protected $primaryKey = 'id';
still have their primary key queries (find()
,findOrFail()
,whereKey()
, etc.) hard-coded to use the MongoDB_id
field. - Disabling
rename_embedded_id_field
has no effect on the top-level_id
behavior. - Route Model Binding and direct lookups by
$model->id
do not work as expected when using a string or UUID primary key namedid
.
Steps to Reproduce:
-
Set up a Laravel model extending
Jenssegers\Mongodb\Eloquent\Model
with:class Invoice extends Model { protected $primaryKey = 'id'; }
-
In
config/database.php
, configure:'connections' => [ 'mongodb' => [ 'driver' => 'mongodb', // ... 'rename_embedded_id_field' => false, ], ],
-
Attempt to retrieve a document via
Invoice::find($idString)
or via route model binding on theid
field.
Expected Behavior:
- The package should respect the model’s
$primaryKey
and use theid
field for top-level queries whenrename_embedded_id_field
is set tofalse
.
Actual Behavior:
- The package continues to query against the
_id
field, ignoring the customprimaryKey
, making it impossible to use a string or UUIDid
as the primary key without overriding core builder methods.
Impact:
This limitation prevents the use of non-ObjectId primary keys at the top level and breaks Laravel features like route model binding for string or UUID keys.
Workarounds:
Currently, the only way to work around this issue is to override multiple internal builder methods (find
, whereKey
, findOrFail
, etc.) or to run native MongoDB queries manually, which is not sustainable.
Request:
Please introduce a new configuration option (e.g., rename_id_field
) or enhance existing logic so that top-level _id
aliasing can be disabled, allowing the package to use the model’s $primaryKey
for all primary key operations.
Activity
GromNaN commentedon May 26, 2025
Thanks for the report!
A few clarifications:
Jenssegers\Mongodb\Eloquent\Model
has been replaced byMongoDB\Laravel\Eloquent\Model
. It looks like the issue report may be mixing package versions._id
values. In fact, Binary UUIDs are recommended for efficiency over their string representations._id
and the custom one), requiring a unique index and extra complexity.We recommend aligning with MongoDB's native
_id
behavior when possible, but we’re open to exploring configuration options to support more advanced use cases.Just to understand better: are you using "id" as the custom primary key because Laravel requires it for features like route model binding or other internal behaviors? Or is it a design choice on your end? Knowing whether this is a Laravel constraint would help clarify if the issue is something we can address within the MongoDB driver or if it stems from framework-level expectations.