2025-06-27 14:19:40 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Resources;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
class RoomResource extends JsonResource
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Transform the resource into an array.
|
|
|
|
*
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
|
|
|
public function toArray(Request $request): array
|
|
|
|
{
|
|
|
|
$array = parent::toArray($request);
|
|
|
|
$array['started_at'] = $this->formatTaipei($this->started_at);
|
|
|
|
$array['ended_at'] = $this->formatTaipei($this->ended_at);
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function formatTaipei(?Carbon $value): ?string
|
|
|
|
{
|
2025-06-27 14:36:22 +08:00
|
|
|
if ($value instanceof \Carbon\Carbon) {
|
|
|
|
return $value->timezone('Asia/Taipei')->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果已經是字串(例如 JSON 解開的資料)
|
|
|
|
if (is_string($value)) {
|
|
|
|
try {
|
|
|
|
return \Carbon\Carbon::parse($value)->timezone('Asia/Taipei')->format('Y-m-d H:i:s');
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2025-06-27 14:19:40 +08:00
|
|
|
}
|
|
|
|
}
|