where(['shop_id' => $row['shop_id']])->count('*'); $sum_star = Db::name('xilufitness_user_comment')->where(['shop_id' => $row['shop_id']])->sum('star'); $avage_star = $count > 0 ? $sum_star/$count : 0; Db::name('xilufitness_shop')->where(['id' => $row['shop_id'] ?? 0])->update(['star' => $avage_star]); }); } /** * 关联用户 */ public function user(){ return $this->belongsTo(User::class,'user_id','id',[],'LEFT')->setEagerlyType(0); } }