refactor the queries

This commit is contained in:
2025-02-09 09:30:49 +03:30
parent 18cbf77261
commit c8ff302d09
3 changed files with 19 additions and 21 deletions

View File

@@ -71,27 +71,23 @@ class RoadPatrolReportController extends Controller
public function activitiesOnMap(Request $request): JsonResponse
{
$date_from = $request->date_from ? $request->date_from .' 00:00:00' : Date('Y-m-d') .' 00:00:00';
$date_to = $request->date_to ? $request->date_to.' 23:59:59' : (new \DateTime('tomorrow'))->format('Y-m-d').' 23:59:59';
$date_from = $request->date_from ? $request->date_from .' 00:00:00' : now()->startOfDay()->toDateTimeString();
$date_to = $request->date_to ? $request->date_to.' 23:59:59' : now()->addDay()->startOfDay()->toDateTimeString();
$provinceId = $request->province_id;
$edareId = $request->edare_id;
$data = RoadPatrol::query()->when($request->province_id, function ($query, $province) {
return $query->where('province_id', '=', $province);
})
->when($request->edare_id, function ($query, $edare_id) {
return $query->where('edare_id', '=', $edare_id);
})
->whereBetween('created_at', [
$date_from,
$date_to,
])
$data = RoadPatrol::query()
->when($provinceId, fn($query, $provinceId) => $query->where('province_id', '=', $provinceId))
->when($edareId, fn($query, $edareId) => $query->where('edare_id', '=', $edareId))
->whereBetween('created_at', [$date_from, $date_to])
->select('id', 'start_lat', 'start_lon')
->get();
return $this->successResponse($data);
}
public function showOnMap(RoadPatrol $road_patrol)
public function showOnMap(RoadPatrol $roadPatrol): JsonResponse
{
return $this->successResponse($road_patrol->load(['rahdaran:id,name,code', 'cmmsMachines:id,machine_code,car_name,plak_number']));
return $this->successResponse($roadPatrol->load(['rahdaran:id,name,code', 'cmmsMachines:id,machine_code,car_name,plak_number']));
}
}