Skip to content

Commit ac77d91

Browse files
committed
added handling for arrays of objectids
1 parent 8c5fbaf commit ac77d91

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,16 @@ protected function getAttributeFromArray($key)
165165
public function setAttribute($key, $value)
166166
{
167167
// Convert _id to ObjectID.
168-
if (is_string($value) && ($key === '_id' || $this->isObjectId($key))) {
168+
if ((is_string($value) && $key === '_id') || $this->isObjectId($key)) {
169169
$builder = $this->newBaseQueryBuilder();
170170

171-
$value = $builder->convertKey($value);
171+
if (is_array($value)) {
172+
foreach ($value as &$val) {
173+
$val = $builder->convertKey($val);
174+
}
175+
} else {
176+
$value = $builder->convertKey($value);
177+
}
172178
} // Support keys in dot notation.
173179
elseif (Str::contains($key, '.')) {
174180
if (in_array($key, $this->getDates()) && $value) {
@@ -491,6 +497,14 @@ protected function castAttribute($key, $value)
491497
}
492498

493499
if ($this->getCastType($key) === 'objectid') {
500+
if (is_array($value)) {
501+
foreach ($value as &$val) {
502+
$val = (string) $val;
503+
}
504+
505+
return $value;
506+
}
507+
494508
return (string) $value;
495509
}
496510

0 commit comments

Comments
 (0)