Skip to content

Commit f8605e7

Browse files
Add withEmbeds property to control which relationships to load
1 parent cb3dacc commit f8605e7

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/Eloquent/EmbedsRelations.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace MongoDB\Laravel\Eloquent;
66

7-
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Support\Str;
98
use MongoDB\Laravel\Relations\EmbedsMany;
109
use MongoDB\Laravel\Relations\EmbedsOne;
1110
use MongoDB\Laravel\Relations\EmbedsOneOrMany;
1211

13-
use function array_keys;
1412
use function class_basename;
1513
use function debug_backtrace;
1614
use function is_a;
@@ -22,10 +20,17 @@
2220
*/
2321
trait EmbedsRelations
2422
{
23+
/**
24+
* The embeds relations to load on every query.
25+
*
26+
* @var array
27+
*/
28+
protected $withEmbeds = [];
29+
2530
public static function bootEmbedsRelations(): void
2631
{
2732
static::retrieved(function (self $model) {
28-
$model->withEmbedded();
33+
$model->load($model->withEmbeds);
2934
});
3035
}
3136

@@ -97,28 +102,6 @@ protected function embedsOne($related, $localKey = null, $foreignKey = null, $re
97102
return new EmbedsOne($query, $this, $instance, $localKey, $foreignKey, $relation);
98103
}
99104

100-
/**
101-
* Load embedded relations on the model if they are not already loaded
102-
*
103-
* @param array|string $relations
104-
*
105-
* @return Model
106-
*/
107-
public function withEmbedded($relations = [])
108-
{
109-
if (empty($relations)) {
110-
$relations = [];
111-
112-
foreach (array_keys($this->getAttributes()) as $key) {
113-
if ($this->isEmbeddedRelation($key)) {
114-
$relations[] = $key;
115-
}
116-
}
117-
}
118-
119-
return $this->loadMissing($relations);
120-
}
121-
122105
/**
123106
* Determine if the given key is an embed relationship method on the model.
124107
*

tests/EmbeddedRelationsTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use MongoDB\BSON\ObjectId;
1212
use MongoDB\Laravel\Tests\Models\Address;
1313
use MongoDB\Laravel\Tests\Models\User;
14+
use MongoDB\Laravel\Tests\Models\UserWithEmbeds;
1415

1516
use function array_merge;
1617

@@ -20,6 +21,7 @@ public function tearDown(): void
2021
{
2122
Mockery::close();
2223
User::truncate();
24+
UserWithEmbeds::truncate();
2325

2426
parent::tearDown();
2527
}
@@ -975,11 +977,11 @@ public function testUnsetPropertyOnEmbed()
975977

976978
public function testEmbedManyToArrayCast()
977979
{
978-
$user = User::create(['name' => 'John Doe']);
980+
$user = UserWithEmbeds::create(['name' => 'John Doe']);
979981
$user->addresses()->saveMany([new Address(['city' => 'London'])]);
980982

981983
//Reload document
982-
$user = User::where('name', 'John Doe')->first();
984+
$user = UserWithEmbeds::where('name', 'John Doe')->first();
983985
$array = $user->toArray();
984986

985987
$this->assertIsString($array['addresses'][0]['id']);

tests/Models/UserWithEmbeds.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Laravel\Tests\Models;
6+
7+
class UserWithEmbeds extends User
8+
{
9+
protected $withEmbeds = ['addresses'];
10+
}

0 commit comments

Comments
 (0)