-
Type: Bug
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Laravel
zliebersbach has created Issue #2986: whereHas does not work with ObjectId relations in laravel-mongodb. This Jira ticket was filed by GromNaN
Issue Text:
- 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
1. `EntityB` has the following relation configured:
```php
protected $casts = [
'entity_a_id' => ObjectId::class,
];
public function entityA(): BelongsTo
{ return $this->belongsTo(EntityA::class); }```
2. `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(
,{"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(
,{"projection":
{"_id":true},"typeMap":{"root":"array","document":"array"}})"
"entity_b.find({"entity_a_id":{"$in":["66591310b42a652a7c027862","66591310b42a652a7c027863"]}},{"typeMap":{"root":"array","document":"array"}})"
```
<details><summary><b>Logs</b>:</summary>
</details>