has_one.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 string $relationship
  30. * @param array $parents
  31. * @param array $children
  32. * @return void
  33. */
  34. public function match($relationship, &$parents, $children)
  35. {
  36. $foreign = $this->foreign_key();
  37. foreach ($parents as &$parent)
  38. {
  39. $matching = array_first($children, function($k, $v) use (&$parent, $foreign)
  40. {
  41. return $v->$foreign == $parent->get_key();
  42. });
  43. $parent->relationships[$relationship] = $matching;
  44. }
  45. }
  46. }