Skip to content

Commit

Permalink
[HasMany] Ensure it returns a collection even if parent does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
TZK- authored and camillebaronnet committed Oct 6, 2020
1 parent 171d3ad commit 8f131e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Bridges/Laravel/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}
11 changes: 11 additions & 0 deletions src/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 8f131e7

Please sign in to comment.