has_one.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php namespace Laravel\Database\Eloquent\Relationships;
  2. class Has_One extends Has_One_Or_Many {
  3. /**
  4. * Get the properly hydrated results for the relationship.
  5. *
  6. * @return Model
  7. */
  8. public function results()
  9. {
  10. return parent::first();
  11. }
  12. /**
  13. * Initialize a relationship on an array of parent models.
  14. *
  15. * @param array $parents
  16. * @param string $relationship
  17. * @return void
  18. */
  19. public function initialize(&$parents, $relationship)
  20. {
  21. foreach ($parents as &$parent)
  22. {
  23. $parent->relationships[$relationship] = null;
  24. }
  25. }
  26. /**
  27. * Match eagerly loaded child models to their parent models.
  28. *
  29. * @param array $parents
  30. * @param array $children
  31. * @return void
  32. */
  33. public function match($relationship, &$parents, $children)
  34. {
  35. $foreign = $this->foreign_key();
  36. foreach ($parents as &$parent)
  37. {
  38. $matching = array_first($children, function($k, $v) use ($parent, $foreign)
  39. {
  40. return $v->$foreign == $parent->get_key();
  41. });
  42. $parent->relationships[$relationship] = $matching;
  43. }
  44. }
  45. }