41 lines
1.3 KiB
PHP
41 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1\Chat\Resources;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class ConversationResource extends JsonResource
|
|
{
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(Request $request): array
|
|
{
|
|
$otherUser = $this->relationLoaded('users')
|
|
? $this->users->firstWhere('id', '!=', $request->user()?->id)
|
|
: null;
|
|
|
|
$channel = $this->relationLoaded('channel') ? $this->channel : null;
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'thread_key' => $this->thread_key,
|
|
'user' => $this->when($otherUser, fn () => new ChatUserResource($otherUser)),
|
|
'channel' => $channel ? [
|
|
'id' => $channel->id,
|
|
'name' => $channel->name,
|
|
'slug' => $channel->slug,
|
|
'image' => $channel->thumbnail('200x200'),
|
|
] : null,
|
|
'last_message' => $this->when(
|
|
$this->relationLoaded('latestMessage') && $this->latestMessage,
|
|
fn () => new MessageResource($this->latestMessage)
|
|
),
|
|
'unread_count' => (int) ($this->unread_count ?? 0),
|
|
'created_at' => $this->created_at,
|
|
'updated_at' => $this->updated_at,
|
|
];
|
|
}
|
|
}
|