\App\Module\GameItems\Casts\DisplayAttributesCast::class, 'numeric_attributes' => \App\Module\GameItems\Casts\NumericAttributesCast::class, 'type' => ITEM_TYPE::class, 'is_unique' => 'boolean', 'tradable' => 'boolean', 'dismantlable' => 'boolean', ]; /** * 获取物品所属分类 * * @return BelongsTo */ public function category(): BelongsTo { return $this->belongsTo(ItemCategory::class, 'category_id'); } /** * 获取物品的所有实例 * * @return HasMany */ public function instances(): HasMany { return $this->hasMany(ItemInstance::class, 'item_id'); } /** * 获取拥有该物品的用户 * * @return HasMany */ public function users(): HasMany { return $this->hasMany(ItemUser::class, 'item_id'); } /** * 判断物品是否为宝箱 * * @return bool */ public function isChest(): bool { return $this->type == 5; // 5表示宝箱类型 } /** * 获取宝箱内容配置 * * @return HasMany */ public function chestContents(): HasMany { if (!$this->isChest()) { return $this->hasMany(ItemChestContent::class, 'chest_id')->whereRaw('1 = 0'); // 返回空集合 } return $this->hasMany(ItemChestContent::class, 'chest_id'); } /** * 检查物品是否已过期(全局过期) * * @return bool */ public function isExpired(): bool { if (empty($this->global_expire_at)) { return false; } return $this->global_expire_at->isPast(); } }