KTVCentral/app/Livewire/Grids/RoomGrid.php

44 lines
1010 B
PHP

<?php
namespace App\Livewire\Grids;
use App\Models\Room;
use App\Models\Branch;
use App\Enums\RoomType;
use Livewire\Component;
use Illuminate\Database\Eloquent\Collection;
class RoomGrid extends Component
{
public $branchName="";
public array $roomTypes;
public function mount()
{
$this->roomTypes = ['all' => '全部'] + collect(RoomType::cases())->mapWithKeys(fn($e) => [$e->value => $e->labels()])->toArray();
}
public function render()
{
$branch = Branch::first();
$this->branchName = $branch->name ?? '';
$rooms = collect(); // 預設為空集合
$floors = [];
if ($branch) {
$rooms = Room::where('branch_id', $branch->id)->orderBy('name', 'asc')->get();
$floors = $rooms->pluck('floor')->unique()->sort()->values()->toArray();
}
return view('livewire.grids.room-grid', [
'rooms' => $rooms,
'floors' => $floors,
]);
}
}