From 8f131e7821d02c478abffb76fdca46e0548185f5 Mon Sep 17 00:00:00 2001 From: Thibault GRANADA <7808125+TZK-@users.noreply.github.com> Date: Tue, 6 Oct 2020 10:15:11 +0200 Subject: [PATCH] [HasMany] Ensure it returns a collection even if parent does not exist #15 --- src/Bridges/Laravel/Relations/HasMany.php | 12 +++++++----- src/Relations/HasMany.php | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Bridges/Laravel/Relations/HasMany.php b/src/Bridges/Laravel/Relations/HasMany.php index d39cff8..c4e18af 100644 --- a/src/Bridges/Laravel/Relations/HasMany.php +++ b/src/Bridges/Laravel/Relations/HasMany.php @@ -6,6 +6,7 @@ use Cristal\ApiWrapper\Relations\HasMany as CoreHasMany; use Illuminate\Database\Eloquent\Model as ModelEloquent; use LogicException; +use Illuminate\Support\Collection; class HasMany extends CoreHasMany { @@ -26,12 +27,13 @@ public function __construct($parent, Model $related, $foreignKey, $localKey) $this->addConstraints(); } - /** - * @param $data - * @return mixed - */ public function getRelationsFromArray($data) { - return collect(parent::getRelationsFromArray($data)); + return new Collection(parent::getRelationsFromArray($data)); + } + + public function getResults() + { + return new Collection(parent::getResults()); } } diff --git a/src/Relations/HasMany.php b/src/Relations/HasMany.php index 9c27b3e..0b5bd81 100755 --- a/src/Relations/HasMany.php +++ b/src/Relations/HasMany.php @@ -49,4 +49,15 @@ public function getRelationsFromArray($data) return new $class($item, isset($item[$this->localKey])); }, $data); } + + public function getResults() + { + $results = parent::getResults(); + + if (null === $results) { + return []; + } + + return $results; + } }