HasUniqueIds.php 1012 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Concerns;
  3. trait HasUniqueIds
  4. {
  5. /**
  6. * Indicates if the model uses unique ids.
  7. *
  8. * @var bool
  9. */
  10. public $usesUniqueIds = false;
  11. /**
  12. * Determine if the model uses unique ids.
  13. *
  14. * @return bool
  15. */
  16. public function usesUniqueIds()
  17. {
  18. return $this->usesUniqueIds;
  19. }
  20. /**
  21. * Generate unique keys for the model.
  22. *
  23. * @return void
  24. */
  25. public function setUniqueIds()
  26. {
  27. foreach ($this->uniqueIds() as $column) {
  28. if (empty($this->{$column})) {
  29. $this->{$column} = $this->newUniqueId();
  30. }
  31. }
  32. }
  33. /**
  34. * Generate a new key for the model.
  35. *
  36. * @return string
  37. */
  38. public function newUniqueId()
  39. {
  40. return null;
  41. }
  42. /**
  43. * Get the columns that should receive a unique identifier.
  44. *
  45. * @return array
  46. */
  47. public function uniqueIds()
  48. {
  49. return [];
  50. }
  51. }