Skip to content
Discussion options

You must be logged in to vote

@austineonyema

Hey — I feel your pain. This usually happens because Laravel is inserting exactly what you pass in (a PHP string), and MongoDB won’t “auto-cast” it to an ObjectId for you.

To store an actual ObjectId, you must pass a MongoDB\BSON\ObjectId instance (or define a cast/mutator that converts strings to ObjectIds before insert/update).

Ex(Insert & Update)

use MongoDB\BSON\ObjectId;

Model::create([
  'product_cache_id' => new ObjectId($idString),
]);

// or update
$m->product_cache_id = new ObjectId($idString);
$m->save();

if you want to querying by _id , try this.

Model::where('_id', new ObjectId($idString))->first();

If ObjectId casting isn’t available in your version, a custom…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by GromNaN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants