homeos/app/Livewire/Devices/Index.php

53 lines
1.5 KiB
PHP

<?php
namespace App\Livewire\Devices;
use App\Models\Device;
use App\Models\Room;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Url;
use Livewire\Component;
#[Layout('layouts.app')]
class Index extends Component
{
#[Url(as: 'q')]
public string $search = '';
#[Url]
public string $room = '';
#[Url]
public string $status = '';
public function clearFilters(): void
{
$this->reset('search', 'room', 'status');
}
public function render()
{
$devices = Device::query()
->with(['room', 'entities.state'])
->when($this->search, fn ($q) => $q->where(fn ($w) => $w
->where('name', 'ilike', "%{$this->search}%")
->orWhere('model', 'ilike', "%{$this->search}%")
->orWhere('vendor', 'ilike', "%{$this->search}%")))
->when($this->room, fn ($q) => $q->whereHas('room', fn ($r) => $r->where('uuid', $this->room)))
->orderBy('name')
->get();
if ($this->status === 'online') {
$devices = $devices->filter->isOnline()->values();
} elseif ($this->status === 'offline') {
$devices = $devices->reject->isOnline()->values();
}
return view('livewire.devices.index', [
'groups' => $devices->groupBy(fn ($d) => $d->room?->name ?? __('devices.no_room')),
'total' => $devices->count(),
'rooms' => Room::orderBy('sort')->orderBy('name')->get(),
]);
}
}