fastadmin多城市或者查询方式hasmany
代码示例:// 推荐商品
$pindex = max(1, intval($params['page']));
$psize = 6;
$pstart = ($pindex - 1) * $psize;
$goodslist = Goods::with('prices')->where($where)->where(['status'=>1,'recommend'=>1])->order('weigh', 'desc')->order($pstart, $psize)->select();
if (!empty($goodslist)) {
// 遍历
foreach ($goodslist as &$row) {
// 差异价
if ($this->diypage->groupprice == 1) {
$where['group_id'] = $group_id;
}
// 开启多城市
if ($this->diypage->multiplecity == 1) {
// 市级运营
if ($this->diypage->citytype == 1) {
\think\Log::record('市级运营');
$where['pcity_id'] = intval($params['pcity_id']);
$where['ccity_id'] = intval($params['ccity_id']);
if ($where['pcity_id'] > 0 && $where['ccity_id'] > 0) {
// 多规格差异价
if ($row->hasoption == 1) {
// 商品规格
$options = $row->prices()->where('status', 1)
->where(function($query) use ($where) {
$query->where(function($q) use ($where) {
$q->where($where);
})
->whereOr(function($q) {
$q->where(['pcity_id'=>0,'ccity_id'=>0]);
});
})->fetchSql(true)->select();
// \think\Log::record($options);
if (!empty($options)) {
$row->saleprice = $row->prices()->where('status', 1)
->where(function($query) use ($where) {
$query->where(function($q) use ($where) {
$q->where($where);
})
->whereOr(function($q) {
$q->where(['pcity_id'=>0,'ccity_id'=>0]);
});
})->min('saleprice');
}
} else {// 无规格差异价
// 城市价
$goodsprice = $row->prices()->where('status', 1)
->where(function($query) use ($where) {
$query->where(function($q) use ($where) {
$q->where($where);
})
->whereOr(function($q) {
$q->where(['pcity_id'=>0,'ccity_id'=>0]);
});
})->find();
if (!empty($goodsprice)) {
$row->saleprice = $goodsprice->saleprice;
}
}
} else {
}
} else {// 县级运营
// \think\Log::record('县级运营');
$where['pcity_id'] = $params['pcity_id'];
$where['ccity_id'] = $params['ccity_id'];
$where['scity_id'] = $params['scity_id'];
if ($where['pcity_id'] > 0 && $where['ccity_id'] > 0 && $where['scity_id'] > 0) {
// 多规格差异价
if ($row->hasoption == 1) {
// 商品规格
$options = $row->prices()->where('status', 1)
->where(function($query) use ($where) {
$query->where(function($q) use ($where) {
$q->where($where);
})
->whereOr(function($q) {
$q->where(['pcity_id'=>0,'ccity_id'=>0,'scity_id'=>0]);
});
})->select();
if (!empty($options)) {
$row->saleprice = $row->prices()->where('status', 1)
->where(function($query) use ($where) {
$query->where(function($q) use ($where) {
$q->where($where);
})
->whereOr(function($q) {
$q->where(['pcity_id'=>0,'ccity_id'=>0,'scity_id'=>0]);
});
})->min('saleprice');
}
} else {// 无规格差异价
// 城市价
$goodsprice = $row->prices()->where('status', 1)
->where(function($query) use ($where) {
$query->where(function($q) use ($where) {
$q->where($where);
})
->whereOr(function($q) {
$q->where(['pcity_id'=>0,'ccity_id'=>0,'scity_id'=>0]);
});
})->find();
// \think\Log::record($goodsprice);
if (!empty($goodsprice)) {
$row->saleprice = $goodsprice->saleprice;
}
}
} else {
}
}
} else {// 未开启多城市
// 商品ID
$where['goods_id'] = $row->id;
// 多规格差异价
if ($row->hasoption == 1) {
// 商品规格
$options = $row->prices()->where($where)->select();
if (!empty($options)) {
$row->saleprice = $row->prices()->where($where)->min('saleprice');
}
} else {// 无规格差异价
// 城市价
$goodsprice = $row->prices()->where($where)->find();
if (!empty($goodsprice)) {
$row->saleprice = $goodsprice->saleprice;
}
}
}
}
unset($row);
$return['goodslist'] = $goodslist;
$return['page'] = intval($params['page']) + 1;
}模型:
public function prices()
{
return $this->hasMany('GoodsPrice', 'goods_id', 'id');
}
页:
[1]