Skip to content

Commit 8f131e7

Browse files
TZK-camillebaronnet
authored andcommitted
[HasMany] Ensure it returns a collection even if parent does not exist #15
1 parent 171d3ad commit 8f131e7

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/Bridges/Laravel/Relations/HasMany.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cristal\ApiWrapper\Relations\HasMany as CoreHasMany;
77
use Illuminate\Database\Eloquent\Model as ModelEloquent;
88
use LogicException;
9+
use Illuminate\Support\Collection;
910

1011
class HasMany extends CoreHasMany
1112
{
@@ -26,12 +27,13 @@ public function __construct($parent, Model $related, $foreignKey, $localKey)
2627
$this->addConstraints();
2728
}
2829

29-
/**
30-
* @param $data
31-
* @return mixed
32-
*/
3330
public function getRelationsFromArray($data)
3431
{
35-
return collect(parent::getRelationsFromArray($data));
32+
return new Collection(parent::getRelationsFromArray($data));
33+
}
34+
35+
public function getResults()
36+
{
37+
return new Collection(parent::getResults());
3638
}
3739
}

src/Relations/HasMany.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ public function getRelationsFromArray($data)
4949
return new $class($item, isset($item[$this->localKey]));
5050
}, $data);
5151
}
52+
53+
public function getResults()
54+
{
55+
$results = parent::getResults();
56+
57+
if (null === $results) {
58+
return [];
59+
}
60+
61+
return $results;
62+
}
5263
}

0 commit comments

Comments
 (0)