領収証発行サービス
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.

109 line
2.8KB

  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\Eloquent\SoftDeletes;
  12. use Illuminate\Database\Query\Builder;
  13. use Illuminate\Foundation\Auth\User as Authenticatable;
  14. use Illuminate\Notifications\Notifiable;
  15. use Illuminate\Support\Facades\DB;
  16. use Laravel\Sanctum\HasApiTokens;
  17. class User extends Authenticatable implements IModelFeature
  18. {
  19. use HasApiTokens, HasFactory, Notifiable, HasUuids, SoftDeletes, ContractFeature;
  20. const COL_NAME_ID = 'id';
  21. const COL_NAME_ROLE = 'role';
  22. const COL_NAME_EMAIL = 'email';
  23. const COL_NAME_NAME = 'name';
  24. const COL_NAME_PASSWORD = 'password';
  25. const COL_NAME_CREATED_BY = ColumnName::CREATED_BY;
  26. const COL_NAME_UPDATED_BY = ColumnName::UPDATED_BY;
  27. const COL_NAME_CREATED_AT = ColumnName::CREATED_AT;
  28. const COL_NAME_UPDATED_AT = ColumnName::UPDATED_AT;
  29. const COL_NAME_DELETED_AT = ColumnName::DELETED_AT;
  30. /**
  31. * The attributes that should be hidden for serialization.
  32. *
  33. * @var array<int, string>
  34. */
  35. protected $hidden = [
  36. self::COL_NAME_PASSWORD,
  37. ];
  38. protected $guarded = [
  39. self::COL_NAME_ID,
  40. self::COL_NAME_CREATED_BY,
  41. self::COL_NAME_UPDATED_BY,
  42. self::COL_NAME_CREATED_AT,
  43. self::COL_NAME_UPDATED_AT,
  44. self::COL_NAME_DELETED_AT,
  45. ];
  46. protected $casts = [
  47. self::COL_NAME_ROLE => UserRole::class,
  48. ];
  49. protected $dispatchesEvents = [
  50. 'created' => CreatedEvent::class,
  51. 'updating' => UpdatingEvent::class,
  52. 'deleted' => DeletedEvent::class,
  53. ];
  54. public static function getBuilder(string $name = 'main'): Builder
  55. {
  56. return DB::table(static::getTableName(), $name);
  57. }
  58. public static function getTableName(): string
  59. {
  60. return (new static)->getTable();
  61. }
  62. public function copy(IModelFeature $from): static
  63. {
  64. $data = $from->getAttributeKeys();
  65. foreach ($data as $key) {
  66. $this->$key = $from->$key;
  67. }
  68. return $this;
  69. }
  70. public function getAttributeKeys(): array
  71. {
  72. return array_values(array_unique(array_merge(array_keys($this->attributesToArray()), $this->hidden)));
  73. }
  74. public function isNotSavedModel(): bool
  75. {
  76. return data_get($this, ColumnName::ID) === null;
  77. }
  78. public function getHistory(): ?HistoryModel
  79. {
  80. return new UserHistory();
  81. }
  82. public function getModelName(): string
  83. {
  84. return "ユーザー情報";
  85. }
  86. public function getChangeLogMessage($before, $after): ?string
  87. {
  88. return null;
  89. }
  90. }