| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace App\Module\Pet\Models;
- use UCore\ModelCore;
- use App\Module\Pet\Casts\DisplayAttributesCast;
- use App\Module\Pet\Casts\NumericAttributesCast;
- use App\Module\Pet\Models\PetSkill;
- use App\Module\Pet\Models\PetUser;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 宠物等级配置模型
- *
- * field start
- * @property int $id
- * @property int $pet_id 宠物 ID
- * @property int $level 等级
- * @property int $exp_required 升级所需经验值
- * @property array $skills 可用技能
- * @property \App\Module\Pet\Casts\DisplayAttributesCast $display_attributes 等级显示属性配置
- * @property \App\Module\Pet\Casts\NumericAttributesCast $numeric_attributes 等级数值属性配置
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * field end
- */
- class PetLevelConfig extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'pet_level_configs';
- // attrlist start
- protected $fillable = [
- 'id',
- 'pet_id',
- 'level',
- 'exp_required',
- 'skills',
- 'display_attributes',
- 'numeric_attributes',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'level' => 'integer',
- 'exp_required' => 'integer',
- 'skills' => 'array',
- 'display_attributes' => DisplayAttributesCast::class,
- 'numeric_attributes' => NumericAttributesCast::class,
- ];
- /**
- * 获取关联的宠物
- *
- * @return BelongsTo
- */
- public function pet(): BelongsTo
- {
- return $this->belongsTo(PetUser::class, 'pet_id');
- }
- /**
- * 获取该等级可用的技能
- *
- * 通过 skills 字段中存储的技能ID数组关联到 PetSkill 模型
- *
- * @return \Illuminate\Database\Eloquent\Collection
- */
- public function getAvailableSkills()
- {
- if (empty($this->skills)) {
- return collect();
- }
- return PetSkill::whereIn('id', $this->skills)->get();
- }
- /**
- * 获取技能列表的访问器(带链接)
- *
- * @return string
- */
- public function getSkillsListAttribute()
- {
- $skills = $this->getAvailableSkills();
- if ($skills->isEmpty()) {
- return '<span class="text-muted">无可用技能</span>';
- }
- $skillLinks = [];
- foreach ($skills as $skill) {
- // 生成技能详情页面的链接
- $detailUrl = admin_url("pet-skills/{$skill->id}");
- $skillLinks[] = "<a href=\"{$detailUrl}\" target=\"_blank\" class=\"btn btn-xs btn-primary\" style=\"margin: 2px;\">" .
- "<i class=\"fa fa-eye\"></i> {$skill->skill_name}</a>";
- }
- return implode(' ', $skillLinks);
- }
- /**
- * 检查指定技能是否在该等级可用
- *
- * @param int $skillId 技能ID
- * @return bool
- */
- public function hasSkill(int $skillId): bool
- {
- return !empty($this->skills) && in_array($skillId, $this->skills);
- }
- }
|