HasCompositePrimaryKey.php 757 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace App\Traits;
  3. use Illuminate\Database\Eloquent\Builder;
  4. trait HasCompositePrimaryKey
  5. {
  6. /**
  7. * Get the value indicating whether the IDs are incrementing.
  8. *
  9. * @return bool
  10. */
  11. public function getIncrementing()
  12. {
  13. return false;
  14. }
  15. /**
  16. * Set the keys for a save update query.
  17. *
  18. * @param Builder $query
  19. * @return Builder
  20. */
  21. protected function setKeysForSaveQuery(Builder $query)
  22. {
  23. foreach ($this->getKeyName() as $key) {
  24. if ($this->$key)
  25. $query->where($key, '=', $this->$key);
  26. else
  27. throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);
  28. }
  29. return $query;
  30. }
  31. }