Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,22 @@ public function refresh()

return $this;
}

/**
* @inheritDoc
*/
public function toArray()
{
$embeds = [];

foreach (array_keys($this->getAttributes()) as $key) {
if ($this->isEmbedRelation($key)) {
$embeds[] = $key;
}
}

$this->loadMissing($embeds);

return parent::toArray();
}
}
15 changes: 14 additions & 1 deletion src/Eloquent/EmbedsRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -85,4 +85,17 @@ 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);
}
}
Loading