原始数据结构:市区>区县>公司>设备
需求:给定市的code码,只要该市的所有公司信息及公司设备信息。
开干!!!
基本思路:
1.给定市的code码,获取该市下面的所有区/县,存入数组(areaCode)。
2.公司和设备信息通过关联预载入一起加载,再使用嵌套查询IN,判断是否在以上的数组中。
第一步:获取市下面的区县code;
public static function getOrganiseAndVehicleByCity($code){
$areaCode = self::where('parent_code',$code)->column('code');
}
第二步:关联预载入
class OrganiseModel extends BaseModel
{
public function equipment()
{
return $this->hasMany('EquipmentModel','XXXX','XXXX');
}
}
第三步:嵌套查询
public static function getOrganiseAndVehicleByCity($code){
$areaCode = self::where('parent_code',$code)->column('code');
$info = OrganiseModel::with(['equipment'])->where('region_code','IN',$areaCode)->select();
return $info;
}
就可以啦!
如果有更好的方法,还请各位大神,指点指点。