MorphOneOrMany.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Relations;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. abstract class MorphOneOrMany extends HasOneOrMany
  6. {
  7. /**
  8. * The foreign key type for the relationship.
  9. *
  10. * @var string
  11. */
  12. protected $morphType;
  13. /**
  14. * The class name of the parent model.
  15. *
  16. * @var string
  17. */
  18. protected $morphClass;
  19. /**
  20. * Create a new morph one or many relationship instance.
  21. *
  22. * @param \Illuminate\Database\Eloquent\Builder $query
  23. * @param \Illuminate\Database\Eloquent\Model $parent
  24. * @param string $type
  25. * @param string $id
  26. * @param string $localKey
  27. * @return void
  28. */
  29. public function __construct(Builder $query, Model $parent, $type, $id, $localKey)
  30. {
  31. $this->morphType = $type;
  32. $this->morphClass = $parent->getMorphClass();
  33. parent::__construct($query, $parent, $id, $localKey);
  34. }
  35. /**
  36. * Set the base constraints on the relation query.
  37. *
  38. * @return void
  39. */
  40. public function addConstraints()
  41. {
  42. if (static::$constraints) {
  43. $this->getRelationQuery()->where($this->morphType, $this->morphClass);
  44. parent::addConstraints();
  45. }
  46. }
  47. /**
  48. * Set the constraints for an eager load of the relation.
  49. *
  50. * @param array $models
  51. * @return void
  52. */
  53. public function addEagerConstraints(array $models)
  54. {
  55. parent::addEagerConstraints($models);
  56. $this->getRelationQuery()->where($this->morphType, $this->morphClass);
  57. }
  58. /**
  59. * Create a new instance of the related model. Allow mass-assignment.
  60. *
  61. * @param array $attributes
  62. * @return \Illuminate\Database\Eloquent\Model
  63. */
  64. public function forceCreate(array $attributes = [])
  65. {
  66. $attributes[$this->getForeignKeyName()] = $this->getParentKey();
  67. $attributes[$this->getMorphType()] = $this->morphClass;
  68. return $this->related->forceCreate($attributes);
  69. }
  70. /**
  71. * Set the foreign ID and type for creating a related model.
  72. *
  73. * @param \Illuminate\Database\Eloquent\Model $model
  74. * @return void
  75. */
  76. protected function setForeignAttributesForCreate(Model $model)
  77. {
  78. $model->{$this->getForeignKeyName()} = $this->getParentKey();
  79. $model->{$this->getMorphType()} = $this->morphClass;
  80. }
  81. /**
  82. * Get the relationship query.
  83. *
  84. * @param \Illuminate\Database\Eloquent\Builder $query
  85. * @param \Illuminate\Database\Eloquent\Builder $parentQuery
  86. * @param array|mixed $columns
  87. * @return \Illuminate\Database\Eloquent\Builder
  88. */
  89. public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
  90. {
  91. return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where(
  92. $query->qualifyColumn($this->getMorphType()), $this->morphClass
  93. );
  94. }
  95. /**
  96. * Get the foreign key "type" name.
  97. *
  98. * @return string
  99. */
  100. public function getQualifiedMorphType()
  101. {
  102. return $this->morphType;
  103. }
  104. /**
  105. * Get the plain morph type name without the table.
  106. *
  107. * @return string
  108. */
  109. public function getMorphType()
  110. {
  111. return last(explode('.', $this->morphType));
  112. }
  113. /**
  114. * Get the class name of the parent model.
  115. *
  116. * @return string
  117. */
  118. public function getMorphClass()
  119. {
  120. return $this->morphClass;
  121. }
  122. }