Open
Description
- Laravel-mongodb Version: 4.3.0
- PHP Version: 8.2.18
- Database Driver & Version: 1.18
Description:
Cannot use whereHas on relations that use ObjectId. ObjectId is the best-practice method for storing related document IDs.
Loading the relation works as expected, but not whereHas.
If this is possible, I propose adding the solution to the documentation, otherwise a fix should be introduced.
Steps to reproduce
EntityB
has the following relation configured:
protected $casts = [
'entity_a_id' => ObjectId::class,
];
public function entityA(): BelongsTo
{
return $this->belongsTo(EntityA::class);
}
EntityB::whereHas('entityA', fn ($query) => $query->where('status', Status::ADDED))->get()
Expected behaviour
The following query should be executed (collected from DB::getQueryLog()
):
"entity_a.find({"status":"A"},{"projection":{"_id":true},"typeMap":{"root":"array","document":"array"}})"
"entity_b.find({"entity_a_id":{"$in":[ObjectId("66591310b42a652a7c027862"),ObjectId("66591310b42a652a7c027863")]}},{"typeMap":{"root":"array","document":"array"}})"
Actual behaviour
The following query is executed (collected from DB::getQueryLog()
):
"entity_a.find({"status":"A"},{"projection":{"_id":true},"typeMap":{"root":"array","document":"array"}})"
"entity_b.find({"entity_a_id":{"$in":["66591310b42a652a7c027862","66591310b42a652a7c027863"]}},{"typeMap":{"root":"array","document":"array"}})"