MorphPivot.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Relations;
  3. use Illuminate\Database\Eloquent\Builder;
  4. class MorphPivot extends Pivot
  5. {
  6. /**
  7. * The type of the polymorphic relation.
  8. *
  9. * Explicitly define this so it's not included in saved attributes.
  10. *
  11. * @var string
  12. */
  13. protected $morphType;
  14. /**
  15. * The value of the polymorphic relation.
  16. *
  17. * Explicitly define this so it's not included in saved attributes.
  18. *
  19. * @var string
  20. */
  21. protected $morphClass;
  22. /**
  23. * Set the keys for a save update query.
  24. *
  25. * @param \Illuminate\Database\Eloquent\Builder $query
  26. * @return \Illuminate\Database\Eloquent\Builder
  27. */
  28. protected function setKeysForSaveQuery(Builder $query)
  29. {
  30. $query->where($this->morphType, $this->morphClass);
  31. return parent::setKeysForSaveQuery($query);
  32. }
  33. /**
  34. * Delete the pivot model record from the database.
  35. *
  36. * @return int
  37. */
  38. public function delete()
  39. {
  40. $query = $this->getDeleteQuery();
  41. $query->where($this->morphType, $this->morphClass);
  42. return $query->delete();
  43. }
  44. /**
  45. * Set the morph type for the pivot.
  46. *
  47. * @param string $morphType
  48. * @return $this
  49. */
  50. public function setMorphType($morphType)
  51. {
  52. $this->morphType = $morphType;
  53. return $this;
  54. }
  55. /**
  56. * Set the morph class for the pivot.
  57. *
  58. * @param string $morphClass
  59. * @return \Illuminate\Database\Eloquent\Relations\MorphPivot
  60. */
  61. public function setMorphClass($morphClass)
  62. {
  63. $this->morphClass = $morphClass;
  64. return $this;
  65. }
  66. }