ItemUser.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use UCore\ModelCore;
  5. /**
  6. * 用户物品关联
  7. *
  8. * field start
  9. * @property int $id 记录ID,主键
  10. * @property int $user_id 用户ID
  11. * @property int $item_id 统一属性物品ID,外键关联kku_item_items表
  12. * @property int $instance_id 单独属性物品ID,外键关联kku_item_instances表(可为空)
  13. * @property int $quantity 数量(对于单独属性物品,该值始终为1)
  14. * @property string $expire_at 用户物品过期时间(可为空)
  15. * @property \Carbon\Carbon $created_at 获取时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. */
  19. class ItemUser extends ModelCore
  20. {
  21. /**
  22. * 与模型关联的表名
  23. *
  24. * @var string
  25. */
  26. protected $table = 'item_users';
  27. // attrlist start
  28. protected $fillable = [
  29. 'id',
  30. 'user_id',
  31. 'item_id',
  32. 'instance_id',
  33. 'quantity',
  34. 'expire_at',
  35. ];
  36. // attrlist end
  37. /**
  38. * 应该被转换为日期的属性
  39. *
  40. * @var array
  41. */
  42. protected $dates = [
  43. 'expire_at',
  44. 'created_at',
  45. 'updated_at',
  46. ];
  47. /**
  48. * 获取关联的物品
  49. *
  50. * @return BelongsTo
  51. */
  52. public function item(): BelongsTo
  53. {
  54. return $this->belongsTo(Item::class, 'item_id');
  55. }
  56. /**
  57. * 获取关联的物品实例(如果有)
  58. *
  59. * @return BelongsTo
  60. */
  61. public function instance(): BelongsTo
  62. {
  63. return $this->belongsTo(ItemInstance::class, 'instance_id');
  64. }
  65. /**
  66. * 检查是否为单独属性物品
  67. *
  68. * @return bool
  69. */
  70. public function isUniqueItem(): bool
  71. {
  72. return !empty($this->instance_id);
  73. }
  74. }