tariqbilal's avatar

Where clause not working on mongodb/laravel-mongodb 5.5 laravel 12

  • Laravel-mongodb Version: 5.5
  • PHP Version: 8.3
  • Laravel Version: 12.0
  • Database Driver & Version: MongoDB 4.4

Description:

After upgrading to Laravel 12, the where() clause in Eloquent queries with MongoDB has stopped working. Queries that were functioning correctly in previous Laravel versions now return null or empty results.

Steps to reproduce

  1. Upgrade from Laravel 11 to Laravel 12
  2. Use any Eloquent model with MongoDB connection
  3. Execute a simple where query: Model::where('_id', $id)->first()
  4. Query returns null even when document exists

Expected behaviour

The where clause should return the matching document from MongoDB collection, as it did in previous Laravel versions.

Actual behaviour

The where clause returns null even when documents matching the criteria exist in the database. No errors are thrown, but queries fail silently.

Example Code:

0 likes
2 replies
JussiMannisto's avatar

Your query is for an _id value of 49, but the _id value on the model is 5e2acfb455f1fe5f258b4568. 49 is the value of id, not _id.

tariqbilal's avatar

Thanks for the clarification, @jussimannisto. I'm still on v5.5 — I recently migrated my collection to use a different key instead of the default id. Glad it worked.

For anyone who has upgraded and is still facing issues: this isn’t a bug that can be patched, it’s a technical change in the upgrade. The best approach is to map your old IDs to new keys and update your queries accordingly.

Appreciate the help, @jussimannisto ! 🙌

https://github.com/mongodb/laravel-mongodb/releases/tag/5.0.0

Please or to participate in this conversation.