Skip to content

Feat serialize embeds #3406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
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
33 changes: 32 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,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();
}
}
Loading