Skip to content

Commit 9b02b5d

Browse files
author
Pooya Parsa
committed
model relations
1 parent 726e610 commit 9b02b5d

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Jenssegers/Mongodb/Eloquent/Model.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,29 @@ public function getAttribute($key)
234234

235235
// This attribute matches an embedsOne or embedsMany relation so we need
236236
// to return the relation results instead of the interal attributes.
237-
if ($relations instanceof EmbedsOneOrMany or $relations instanceof MorphTo) {
237+
if ($relations instanceof EmbedsOneOrMany) {
238238
// If the key already exists in the relationships array, it just means the
239239
// relationship has already been loaded, so we'll just return it out of
240240
// here because there is no need to query within the relations twice.
241-
if (array_key_exists($key, $this->relations) && $this->relations[$key] != null) {
241+
if (array_key_exists($key, $this->relations)) {
242242
return $this->relations[$key];
243243
}
244244

245245
// Get the relation results.
246246
return $this->getRelationshipFromMethod($key, $camelKey);
247247
}
248+
249+
if($relations instanceof Relation) {
250+
// If the key already exists in the relationships array, it just means the
251+
// relationship has already been loaded, so we'll just return it out of
252+
// here because there is no need to query within the relations twice.
253+
if (array_key_exists($key, $this->relations) && $this->relations[$key] !=null)
254+
return $this->relations[$key];
255+
256+
// Get the relation results.
257+
return $this->getRelationshipFromMethod($key, $camelKey);
258+
}
259+
248260
}
249261
}
250262

tests/RelationsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ public function testBelongsToManyCustom()
322322
$this->assertTrue(array_key_exists('groups', $user->getAttributes()));
323323

324324
// Assert they are attached
325-
$this->assertTrue(in_array($group->_id, $user->groups));
326-
$this->assertTrue(in_array($user->_id, $group->users));
325+
$this->assertTrue(in_array($group->_id, $user->groups->pluck('_id')->toArray()));
326+
$this->assertTrue(in_array($user->_id, $group->users->pluck('_id')->toArray()));
327327
$this->assertEquals($group->_id, $user->groups()->first()->_id);
328328
$this->assertEquals($user->_id, $group->users()->first()->_id);
329329
}

0 commit comments

Comments
 (0)