|
- <?php
-
- namespace App\Models;
-
- use App\Codes\UserRole;
- use App\Events\Model\CreatedEvent;
- use App\Events\Model\DeletedEvent;
- use App\Events\Model\UpdatingEvent;
- use App\Models\Feature\ContractFeature;
- use App\Models\Feature\IModelFeature;
- use Illuminate\Database\Eloquent\Concerns\HasUuids;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Support\Facades\DB;
- use Laravel\Sanctum\HasApiTokens;
-
- class User extends Authenticatable implements IModelFeature
- {
- use HasApiTokens, HasFactory, Notifiable, HasUuids, ContractFeature;
-
- const COL_NAME_ID = 'id';
- const COL_NAME_ROLE = 'role';
- const COL_NAME_EMAIL = 'email';
- const COL_NAME_NAME = 'name';
- const COL_NAME_PASSWORD = 'password';
-
- const COL_NAME_CREATED_BY = ColumnName::CREATED_BY;
- const COL_NAME_UPDATED_BY = ColumnName::UPDATED_BY;
- const COL_NAME_CREATED_AT = ColumnName::CREATED_AT;
- const COL_NAME_UPDATED_AT = ColumnName::UPDATED_AT;
- const COL_NAME_DELETED_AT = ColumnName::DELETED_AT;
-
- /**
- * The attributes that should be hidden for serialization.
- *
- * @var array<int, string>
- */
- protected $hidden = [
- self::COL_NAME_PASSWORD,
- ];
-
- protected $guarded = [
- self::COL_NAME_ID,
- self::COL_NAME_CREATED_BY,
- self::COL_NAME_UPDATED_BY,
- self::COL_NAME_CREATED_AT,
- self::COL_NAME_UPDATED_AT,
- self::COL_NAME_DELETED_AT,
- ];
-
- protected $casts = [
- self::COL_NAME_ROLE => UserRole::class,
- ];
-
- protected $dispatchesEvents = [
- 'created' => CreatedEvent::class,
- 'updating' => UpdatingEvent::class,
- 'deleted' => DeletedEvent::class,
- ];
-
- public static function getBuilder(string $name = 'main'): Builder
- {
- return DB::table(static::getTableName(), $name);
- }
-
- public static function getTableName(): string
- {
- return (new static)->getTable();
- }
-
- public function copy(IModelFeature $from): static
- {
- $data = $from->getAttributeKeys();
-
- foreach ($data as $key) {
- $this->$key = $from->$key;
- }
- return $this;
- }
-
- public function getAttributeKeys(): array
- {
- return array_values(array_unique(array_merge(array_keys($this->attributesToArray()), $this->hidden)));
- }
-
- public function isNotSavedModel(): bool
- {
- return data_get($this, ColumnName::ID) === null;
- }
-
- public function getHistory(): ?HistoryModel
- {
- return new UserHistory();
- }
-
- public function getModelName(): string
- {
- return "ユーザー情報";
- }
-
- public function getChangeLogMessage($before, $after): ?string
- {
- return null;
- }
- }
|