【发布时间】:2022-01-06 22:36:25
【问题描述】:
假设我在遗留系统中有以下关系,无法改变它。
- 省表(有很多区)
- 地区表(有许多村庄)
- 村表(有很多医院)
- hospital_types 表(hasOne 医院)
- hospitals 表(属于 hospital_types,hasMany 监控)
- 监控表(hasMany rateLog)
我想要的是带医院的省份,并在去年对每家医院进行一次监测。
注意:上一年不是从当前日期算起,而是取每家医院的最后一次监测日期,然后从那个日期开始计算,到那个日期为止需要12个月
假设医院 A 已于 2020 年 11 月 1 日进行最后一次监测,则应在 2020 年 11 月 1 日至 2019 年 11 月 1 日期间完成所有监测。其他医院也一样。
已经用 Laravel 资源实现了,但是有点慢,也不知道在资源返回数据后如何实现 orderBy 功能,因为我需要基于监视的分页和排序,现在我通过资源处理它.
目前我正在使用 hasManyThrough 关系,在 get 之后,我将数据传递给资源集合并处理监控的 get。但是这样就不能实现排序和分页了。
$data = Province::with(['districts.hospitalTypes])->get();
然后我将这些数据传递给资源并获得监控,但是我如何应用排序和分页,因为排序是基于监控的。并且使用资源比急切加载要慢一些。 我正在寻找一种方法来使用带有这些条件的急切负载。
class ProvinceResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'districts' => DistrictResource::collection($this->whenLoaded("districts"))
];
}
}
class DistrictResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'hospitals' => HospitalTypeResource::collection($this->whenLoaded("hospital_types"))
];
}
}
class HospitalTypeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$district = $this->village->district;
$oneLastYearRateLog = $this->hospital->last12MonthRageLogs();
$rateLogCount = $oneLastYearRateLog->count();
$oneLastYearMonitoringCount = $this->hospital->last12MonthMonitors()->count();
return [
"id" => $this->hospital->id,
"code" => $this->code,
"name" => $this->name,
"overallRating"=> $rateLogCount == 0 ? 0 : $oneLastYearRateLog->sum('rate') / $rateLogCount,
"ratingsCount" => $oneLastYearMonitoringCount
];
}
}
下面是我在医院模型中采取监控的关系。
class Hospital extends Model
{
/**
* Get hospital monitors
*/
public function monitors()
{
return $this->hasMany(Monitoring::class, 'hospital_id', 'id');
}
public function last12MonthMonitors()
{
$lastMonitoring = $this->lastMonitoring();
if($lastMonitoring) {
return $this->monitors()->whereRaw('monitoring_date >= "'. $lastMonitoring->monitoring_date.'" - INTERVAL 12 month');
}
return $this->monitors();
}
private function lastMonitoring()
{
return $this->monitors()->select('monitoring_date')->latest('monitoring_date')->first();
}
/**
* Get rate logs
*/
public function rateLogs()
{
return $this->hasManyThrough(RateLog::class, Monitoring::class, 'hospital_id')
->where('rate', '!=', 'NA');
}
/**
* Get last 12 rate logs
*/
public function last12MonthRageLogs()
{
$lastMonitoring = $this->lastMonitoring();
if($lastMonitoring) {
return $this->rateLogs()->whereRaw('monitoring.monitoring_date >= "'.$lastMonitoring->monitoring_date.'" - INTERVAL 12 month');
}
return $this->rateLogs();
}
【问题讨论】:
-
请您显示您的
Resource的代码以及您如何过滤监控。 -
@Rwd 请立即查看
-
你使用的是什么版本的 Laravel?
-
@Rwd Laravel 7,但可以升级到 8
-
你能分享模型的表格结构吗?请将表结构分享为 github gist
标签: php mysql laravel groupwise-maximum