MorphPivot.php 3.7KB

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