diff --git a/src/Eloquent/EmbedsRelations.php b/src/Eloquent/EmbedsRelations.php index d5984b08e..257ce7777 100644 --- a/src/Eloquent/EmbedsRelations.php +++ b/src/Eloquent/EmbedsRelations.php @@ -7,7 +7,7 @@ use Illuminate\Support\Str; use MongoDB\Laravel\Relations\EmbedsMany; use MongoDB\Laravel\Relations\EmbedsOne; - +use MongoDB\Laravel\Relations\EmbedsOneOrMany; use function class_basename; use function debug_backtrace; @@ -85,4 +85,35 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation); } + + /** + * Determine if the given key is an embed relationship method on the model. + * + * @param string $key + * + * @return bool + */ + public function isEmbedRelation($key) + { + return $this->isRelation($key) + && is_a($this->{$key}(), EmbedsOneOrMany::class, true); + } + + /** + * @inheritDoc + */ + public function toArray() + { + $embeds = []; + + foreach (array_keys($this->getAttributes()) as $key) { + if ($this->isEmbedRelation($key)) { + $embeds[] = $key; + } + } + + $this->loadMissing($embeds); + + return parent::toArray(); + } }