1234567891011121314151617181920212223242526272829303132 |
- <?php
- namespace App\Traits;
- /**
- * 状态文本转换 Trait
- */
- trait HasStateText
- {
- /**
- * 获取状态文本
- */
- public function getStateTextAttribute(): string
- {
- if (! isset($this->stateEnumClass)) {
- return '未知状态';
- }
- $enumClass = $this->stateEnumClass;
- return $enumClass::fromValue($this->state)?->label() ?? '未知状态';
- }
- /**
- * 初始化 HasStateText trait
- */
- public function initializeHasStateText(): void
- {
- // 自动添加 state_text 到 appends 数组
- $this->append('state_text');
- }
- }
|