領収証発行サービス
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 line
2.6KB

  1. <?php
  2. namespace App\Models;
  3. use App\Codes\UserRole;
  4. use App\Events\Model\CreatedEvent;
  5. use App\Events\Model\DeletedEvent;
  6. use App\Events\Model\UpdatingEvent;
  7. use App\Models\Feature\ContractFeature;
  8. use App\Models\Feature\IModelFeature;
  9. use Illuminate\Database\Eloquent\Concerns\HasUuids;
  10. use Illuminate\Database\Eloquent\Factories\HasFactory;
  11. use Illuminate\Database\Query\Builder;
  12. use Illuminate\Foundation\Auth\User as Authenticatable;
  13. use Illuminate\Notifications\Notifiable;
  14. use Laravel\Sanctum\HasApiTokens;
  15. class User extends Authenticatable implements IModelFeature
  16. {
  17. use HasApiTokens, HasFactory, Notifiable, HasUuids, ContractFeature;
  18. const COL_NAME_ROLE = 'role';
  19. // public function __construct(...$params)
  20. // {
  21. // $this->dispatchesEvents = [
  22. // 'created' => CreatedListener::class,
  23. // 'updating' => UpdatingListener::class,
  24. // 'deleting' => DeletingListener::class,
  25. // ];
  26. // protected $dispatchesEvents = [
  27. // 'created' => CreatedEvent::class,
  28. // 'updating' => UpdatingEvent::class,
  29. // 'deleted' => DeletedEvent::class,
  30. // ];
  31. // parent::__construct(...$params);
  32. // }
  33. /**
  34. * The attributes that should be hidden for serialization.
  35. *
  36. * @var array<int, string>
  37. */
  38. protected $hidden = [
  39. 'password',
  40. ];
  41. protected $casts = [
  42. self::COL_NAME_ROLE => UserRole::class,
  43. ];
  44. protected $dispatchesEvents = [
  45. 'created' => CreatedEvent::class,
  46. 'updating' => UpdatingEvent::class,
  47. 'deleted' => DeletedEvent::class,
  48. ];
  49. public static function getBuilder(string $name = 'main'): Builder
  50. {
  51. return DB::table(static::getTableName(), $name);
  52. }
  53. public static function getTableName(): string
  54. {
  55. return (new static)->getTable();
  56. }
  57. public function copy(IModelFeature $from): static
  58. {
  59. $data = $from->getAttributeKeys();
  60. foreach ($data as $key) {
  61. $this->$key = $from->$key;
  62. }
  63. return $this;
  64. }
  65. public function getAttributeKeys(): array
  66. {
  67. return array_values(array_unique(array_merge(array_keys($this->attributesToArray()), $this->hidden)));
  68. }
  69. public function isNotSavedModel(): bool
  70. {
  71. return data_get($this, ColumnName::ID) === null;
  72. }
  73. public function getHistory(): ?HistoryModel
  74. {
  75. return new UserHistory();
  76. }
  77. public function getModelName(): string
  78. {
  79. return "ユーザー情報";
  80. }
  81. public function getChangeLogMessage($before, $after): ?string
  82. {
  83. return null;
  84. }
  85. }