has_one.php 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 ($children as $key => $child)
  37. {
  38. $parents[$child->$foreign]->relationships[$relationship] = $child;
  39. }
  40. }
  41. }