MorphMany.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Model\Relations;
  12. use Hyperf\Database\Model\Collection;
  13. class MorphMany extends MorphOneOrMany
  14. {
  15. /**
  16. * Get the results of the relationship.
  17. */
  18. public function getResults()
  19. {
  20. return $this->query->get();
  21. }
  22. /**
  23. * Initialize the relation on a set of models.
  24. *
  25. * @param string $relation
  26. * @return array
  27. */
  28. public function initRelation(array $models, $relation)
  29. {
  30. foreach ($models as $model) {
  31. $model->setRelation($relation, $this->related->newCollection());
  32. }
  33. return $models;
  34. }
  35. /**
  36. * Match the eagerly loaded results to their parents.
  37. *
  38. * @param string $relation
  39. * @return array
  40. */
  41. public function match(array $models, Collection $results, $relation)
  42. {
  43. return $this->matchMany($models, $results, $relation);
  44. }
  45. }