FarmLand.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Module\Farm\Models;
  3. use App\Module\Farm\Enums\LAND_STATUS;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. /**
  8. * 土地信息模型
  9. * field start
  10. * @property int $id 主键ID
  11. * @property int $user_id 用户ID
  12. * @property int $position 土地位置(1-20)
  13. * @property int $land_type 土地类型:1普通,2红土,3黑土,4金,5蓝,6紫
  14. * @property int $status 土地状态:0空闲,1种植中,2灾害,3可收获,4枯萎
  15. * @property \Carbon\Carbon $created_at 创建时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. * @property-read FarmUser $farmUser 关联的用户农场
  19. * @property-read FarmLandType $landType 关联的土地类型
  20. *
  21. * @package App\Module\Farm\Models
  22. */
  23. class FarmLand extends Model
  24. {
  25. /**
  26. * 与模型关联的表名
  27. *
  28. * @var string
  29. */
  30. protected $table = 'farm_land_users';
  31. /**
  32. * 可批量赋值的属性
  33. *
  34. * @var array
  35. */
  36. protected $fillable = [
  37. 'user_id',
  38. 'position',
  39. 'land_type',
  40. 'status',
  41. ];
  42. // 暂时移除枚举类型转换,避免在表单处理时出现类型问题
  43. protected $casts = [
  44. // 'status' => LAND_STATUS::class
  45. ];
  46. /**
  47. * 获取关联的用户农场
  48. *
  49. * @return BelongsTo
  50. */
  51. public function farmUser(): BelongsTo
  52. {
  53. return $this->belongsTo(FarmUser::class, 'user_id', 'user_id');
  54. }
  55. /**
  56. * 获取关联的土地类型
  57. *
  58. * @return BelongsTo
  59. */
  60. public function landType(): BelongsTo
  61. {
  62. return $this->belongsTo(FarmLandType::class, 'land_type', 'id');
  63. }
  64. /**
  65. * 获取关联的作物
  66. *
  67. * @return HasOne
  68. */
  69. public function crop(): HasOne
  70. {
  71. return $this->hasOne(FarmCrop::class, 'land_id', 'id');
  72. }
  73. }