Skip to content

Commit 03aa15d

Browse files
yswerytimgws
authored andcommitted
Fix Relationship loading if there are no relationships found (#79)
Currently we can successfully index a full collection with its respective relationships (some exist and some simply return `null) in laravel.
1 parent 579782d commit 03aa15d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/ElasticquentTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ public static function hydrateRecursive(Model $model, array $items, Relation $pa
688688
$instance = $model;
689689

690690
$items = array_map(function ($item) use ($instance, $parentRelation) {
691+
// Convert all null relations into empty arrays
692+
$item = $item ?: [];
693+
691694
return static::newFromBuilderRecursive($instance, $item, $parentRelation);
692695
}, $items);
693696

@@ -712,7 +715,7 @@ public static function loadRelationsAttributesRecursive(Model $model)
712715

713716
if ($relation instanceof Relation) {
714717
// Check if the relation field is single model or collections
715-
if (!static::isMultiLevelArray($value)) {
718+
if (is_null($value) === true || !static::isMultiLevelArray($value)) {
716719
$value = [$value];
717720
}
718721

0 commit comments

Comments
 (0)