request->post(['land_id','kind','breed','area','user','date','pic','remark']); if(empty($params['land_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['area']) || empty($params['user']) || empty($params['date']) || empty($params['pic'])){ return $this->fail('缺少必要参数'); } if(!strtotime($params['date'])){ return $this->fail('日期格式错误'); } $pics = json_decode($params['pic'],true); if(empty($pics)){ return $this->fail('图片参数格式错误'); } //获取土地信息 $land = Land::field('residual_area')->where('id',$params['land_id'])->findOrEmpty(); if($land->isEmpty()){ return $this->fail('土地信息错误'); } if($params['area'] > $land['residual_area']){ return $this->fail('种植面积超过当前土地可种植面积'); } //创建数据 Db::transaction(function()use($params,$land) { Plant::create([ 'land_id' => $params['land_id'], 'kind' => $params['kind'], 'breed' => $params['breed'], 'area' => $params['area'], 'user' => $params['user'], 'remark' => $params['remark'], 'pic' => $params['pic'], 'plant_date' => strtotime($params['date']), 'status' => 1, 'create_time' => time() ]); $residual_area = $land['residual_area']-$params['area']; Land::where('id',$params['land_id'])->update(['residual_area'=>$residual_area]); }); return $this->success('种植添加成功'); } }