MorphPivot.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Relations;
  3. class MorphPivot extends Pivot
  4. {
  5. /**
  6. * The type of the polymorphic relation.
  7. *
  8. * Explicitly define this so it's not included in saved attributes.
  9. *
  10. * @var string
  11. */
  12. protected $morphType;
  13. /**
  14. * The value of the polymorphic relation.
  15. *
  16. * Explicitly define this so it's not included in saved attributes.
  17. *
  18. * @var string
  19. */
  20. protected $morphClass;
  21. /**
  22. * Set the keys for a save update query.
  23. *
  24. * @param \Illuminate\Database\Eloquent\Builder $query
  25. * @return \Illuminate\Database\Eloquent\Builder
  26. */
  27. protected function setKeysForSaveQuery($query)
  28. {
  29. $query->where($this->morphType, $this->morphClass);
  30. return parent::setKeysForSaveQuery($query);
  31. }
  32. /**
  33. * Set the keys for a select query.
  34. *
  35. * @param \Illuminate\Database\Eloquent\Builder $query
  36. * @return \Illuminate\Database\Eloquent\Builder
  37. */
  38. protected function setKeysForSelectQuery($query)
  39. {
  40. $query->where($this->morphType, $this->morphClass);
  41. return parent::setKeysForSelectQuery($query);
  42. }
  43. /**
  44. * Delete the pivot model record from the database.
  45. *
  46. * @return int
  47. */
  48. public function delete()
  49. {
  50. if (isset($this->attributes[$this->getKeyName()])) {
  51. return (int) parent::delete();
  52. }
  53. if ($this->fireModelEvent('deleting') === false) {
  54. return 0;
  55. }
  56. $query = $this->getDeleteQuery();
  57. $query->where($this->morphType, $this->morphClass);
  58. return tap($query->delete(), function () {
  59. $this->exists = false;
  60. $this->fireModelEvent('deleted', false);
  61. });
  62. }
  63. /**
  64. * Get the morph type for the pivot.
  65. *
  66. * @return string
  67. */
  68. public function getMorphType()
  69. {
  70. return $this->morphType;
  71. }
  72. /**
  73. * Set the morph type for the pivot.
  74. *
  75. * @param string $morphType
  76. * @return $this
  77. */
  78. public function setMorphType($morphType)
  79. {
  80. $this->morphType = $morphType;
  81. return $this;
  82. }
  83. /**
  84. * Set the morph class for the pivot.
  85. *
  86. * @param string $morphClass
  87. * @return \Illuminate\Database\Eloquent\Relations\MorphPivot
  88. */
  89. public function setMorphClass($morphClass)
  90. {
  91. $this->morphClass = $morphClass;
  92. return $this;
  93. }
  94. /**
  95. * Get the queueable identity for the entity.
  96. *
  97. * @return mixed
  98. */
  99. public function getQueueableId()
  100. {
  101. if (isset($this->attributes[$this->getKeyName()])) {
  102. return $this->getKey();
  103. }
  104. return sprintf(
  105. '%s:%s:%s:%s:%s:%s',
  106. $this->foreignKey, $this->getAttribute($this->foreignKey),
  107. $this->relatedKey, $this->getAttribute($this->relatedKey),
  108. $this->morphType, $this->morphClass
  109. );
  110. }
  111. /**
  112. * Get a new query to restore one or more models by their queueable IDs.
  113. *
  114. * @param array|int $ids
  115. * @return \Illuminate\Database\Eloquent\Builder
  116. */
  117. public function newQueryForRestoration($ids)
  118. {
  119. if (is_array($ids)) {
  120. return $this->newQueryForCollectionRestoration($ids);
  121. }
  122. if (! str_contains($ids, ':')) {
  123. return parent::newQueryForRestoration($ids);
  124. }
  125. $segments = explode(':', $ids);
  126. return $this->newQueryWithoutScopes()
  127. ->where($segments[0], $segments[1])
  128. ->where($segments[2], $segments[3])
  129. ->where($segments[4], $segments[5]);
  130. }
  131. /**
  132. * Get a new query to restore multiple models by their queueable IDs.
  133. *
  134. * @param array $ids
  135. * @return \Illuminate\Database\Eloquent\Builder
  136. */
  137. protected function newQueryForCollectionRestoration(array $ids)
  138. {
  139. $ids = array_values($ids);
  140. if (! str_contains($ids[0], ':')) {
  141. return parent::newQueryForRestoration($ids);
  142. }
  143. $query = $this->newQueryWithoutScopes();
  144. foreach ($ids as $id) {
  145. $segments = explode(':', $id);
  146. $query->orWhere(function ($query) use ($segments) {
  147. return $query->where($segments[0], $segments[1])
  148. ->where($segments[2], $segments[3])
  149. ->where($segments[4], $segments[5]);
  150. });
  151. }
  152. return $query;
  153. }
  154. }