ChatResource.php 810 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class ChatResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @return array<string, mixed>
  11. */
  12. public function toArray(Request $request): array
  13. {
  14. $title = $this->title;
  15. if(!$title && $this->created_at->diffInMinutes(now()) > 5) {
  16. $title = 'Новый чат';
  17. }
  18. return array_merge(parent::toArray($request), [
  19. 'title' => $title,
  20. 'messages' => MessageResource::collection($this->whenLoaded('messages')),
  21. 'created_at' => $this->created_at?->format('Y.m.d H:i:s'),
  22. 'updated_at' => $this->updated_at?->format('Y.m.d H:i:s')
  23. ]);
  24. }
  25. }