Browse Source

为FarmLand模型增加has_crop字段判断是否有作物

notfff 7 months ago
parent
commit
808853f8
1 changed files with 18 additions and 1 deletions
  1. 18 1
      app/Module/Farm/Models/FarmLand.php

+ 18 - 1
app/Module/Farm/Models/FarmLand.php

@@ -9,12 +9,13 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
 
 
 /**
 /**
  * 土地信息模型
  * 土地信息模型
- * field start 
+ * field start
  * @property  int  $id  主键ID
  * @property  int  $id  主键ID
  * @property  int  $user_id  用户ID
  * @property  int  $user_id  用户ID
  * @property  int  $position  土地位置(1-20)
  * @property  int  $position  土地位置(1-20)
  * @property  int  $land_type  土地类型:1普通,2红土,3黑土,4金,5蓝,6紫
  * @property  int  $land_type  土地类型:1普通,2红土,3黑土,4金,5蓝,6紫
  * @property  int  $status  土地状态:0空闲,1种植中,2灾害,3可收获,4枯萎
  * @property  int  $status  土地状态:0空闲,1种植中,2灾害,3可收获,4枯萎
+ * @property  bool  $has_crop  是否有作物(种植中、灾害、可收获、枯萎状态都算有作物)
  * @property  \Carbon\Carbon  $created_at  创建时间
  * @property  \Carbon\Carbon  $created_at  创建时间
  * @property  \Carbon\Carbon  $updated_at  更新时间
  * @property  \Carbon\Carbon  $updated_at  更新时间
  * field end
  * field end
@@ -77,4 +78,20 @@ class FarmLand extends Model
     {
     {
         return $this->hasOne(FarmCrop::class, 'land_id', 'id');
         return $this->hasOne(FarmCrop::class, 'land_id', 'id');
     }
     }
+
+    /**
+     * 判断是否有作物
+     * 种植中、灾害、可收获、枯萎状态都算有作物
+     *
+     * @return bool
+     */
+    public function getHasCropAttribute(): bool
+    {
+        return in_array($this->status, [
+            LAND_STATUS::PLANTING->value,    // 1 种植中
+            LAND_STATUS::DISASTER->value,    // 2 灾害
+            LAND_STATUS::HARVESTABLE->value, // 3 可收获
+            LAND_STATUS::WITHERED->value,    // 4 枯萎
+        ]);
+    }
 }
 }