Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions app/Http/Controllers/Table/LocationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@

protected function sortFields($request)
{
return ['location', 'devices', 'down'];
return [
'location',
'devices' => 'devices_count',
'down' => 'down_count',
];
}

/**
Expand All @@ -55,19 +59,10 @@
*/
public function baseQuery($request)
{
// joins are needed for device count sorts
$sort = $request->get('sort');
$key = key($sort);
$join = $this->getJoinQuery($key);

if ($join) {
return Location::hasAccess($request->user())
->select(['id', 'location', 'lat', 'lng', \DB::raw("COUNT(device_id) AS `$key`")])
->leftJoin('devices', $join)
->groupBy(['id', 'location', 'lat', 'lng']);
}

return Location::hasAccess($request->user());
return Location::hasAccess($request->user())->withCount([
'devices',
'devices as down_count' => fn ($q) => (new Device)->scopeIsDown($q),
]);
}

/**
Expand All @@ -81,22 +76,8 @@
'location' => htmlspecialchars((string) $location->location),
'lat' => $location->lat,
'lng' => $location->lng,
'down' => $location->devices()->isDown()->count(),
'devices' => $location->devices()->count(),
'devices' => $location->devices_count,
'down' => $location->down_count,

Check failure on line 80 in app/Http/Controllers/Table/LocationController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Access to an undefined property App\Models\Location::$down_count.
];
}

private function getJoinQuery($field)
{
return match ($field) {
'devices' => function ($query): void {
$query->on('devices.location_id', 'locations.id');
},
'down' => function ($query): void {
$query->on('devices.location_id', 'locations.id');
(new Device)->scopeIsDown($query);
},
default => null,
};
}
}
Loading